搜索部分密钥

时间:2016-03-19 03:00:02

标签: mongodb

我有一个MongoDB集合,其中包含以下格式的文档:

{
    "_id" : ObjectId("56ec4e1bbf2f16a55f287d31"),
    "username" : "John Doe",
    "dates" : {
            "2014-12-24" : 25
    },
}

{
    "_id" : ObjectId("56ec4e1bbf2f16a55f287b32"),
    "username" : "Jane Doe",
    "dates" : {
            "2014-12-27" : 92
    },
}

{
    "_id" : ObjectId("56ec4e1bbf2f16a55f198d31"),
    "username" : "Harry Potter",
    "dates" : {
            "2014-01-31" : 25
    },
}

是否可以对dates执行部分查询,以便从12月开始获取所有记录?例如,我可以db.collection.find({"dates.2014-12-24":{$exists:1}})获取第一个文档但是有没有办法像db.collection.find({"dates.2014-12-*":{$exists:1}})这样做并获得前两个文档?

1 个答案:

答案 0 :(得分:0)

最好重新构建数据库,使其成为值 未知不是关键。

{
    "_id" : ObjectId("56ec4e1bbf2f16a55f198d31"),
    "username" : "Harry Potter",
    "dates" : {
            "key":"2014-01-31",
            "value" : 25
    },
}

现在,您可以对搜索使用regex次查询。

相关问题