根据查询嵌入式文档的mongodb文档,可以通过以与存储文件相同的顺序指定文档的所有字段来进行精确查询。
https://docs.mongodb.org/manual/reference/method/db.collection.find/#query-embedded-documents
db.bios.find(
{
name: {
first: "Yukihiro",
last: "Matsumoto"
}
}
)
这对我来说似乎不起作用。即使我使用find_one检索文档然后复制粘贴它,搜索也不会返回任何内容。
In [57]: TestColl.find_one({'data.local_dt': {'$exists': True}})
Out[57]:
{u'_id': ObjectId('570ddba7f6858f77be9fb8ac'),
u'data': {u'local_dt': {u'hour': 22, u'month': 4, u'year': 2016}}}
In [58]: TestColl.find({'data.local_dt': {u'hour': 22, u'month': 4, u'year': 2016}}).count()
Out[58]: 0
重现的完整步骤:
In [1]: from pymongo import MongoClient
In [2]: TestColl = MongoClient().Stage_database.TestColl
In [3]: TestColl.remove()
Out[3]: {u'n': 2, u'ok': 1}
In [4]: TestColl = MongoClient().Stage_database.TestColl
In [5]: doc = {'data': {'local_dt': {'year': 2016, 'month': 4, 'hour': 22}}}
In [6]: TestColl.insert(doc)
Out[6]: ObjectId('570ddfe7f6858f84169707fd')
In [7]: TestColl.find_one({'data.local_dt': {'$exists': True}})
Out[7]:
{u'_id': ObjectId('570ddfe7f6858f84169707fd'),
u'data': {u'local_dt': {u'hour': 22, u'month': 4, u'year': 2016}}}
In [8]: TestColl.find({'data.local_dt': {u'hour': 22, u'month': 4, u'year': 2016}}).count()
Out[8]: 0
In [9]: TestColl.find({'data.local_dt': {'hour': 22, 'month': 4, 'year': 2016}}).count()
Out[9]: 0
答案 0 :(得分:0)
TestColl.find({'data.local_dt': {'hour': 22, 'month': 4, 'year': 2016}}).count()
你应该使用。符号
TestColl.find({
"data.local_dt.hour": 22,
"data.local_dt.year": 2016,
"data.local_dt.month": 4
})
.count()