我有以下问题。 Pymongo返回的字段少于应有的字段。
这是我的查询:
db.users.findOne({'e.email': 'xxx@gmail.com', application: 'App1'})
直接来自mongo db我得到:
{
"_id" : ObjectId("51803128e4b092fd00c8899b"),
"application": "App1",
"d" : ISODate("2013-04-30T21:01:28.084Z"),
"e" : [
{
"email" : "xxx@gmail.com",
"isValidated" : true
}
],
"fn" : "XXX",
"l" : "en_US",
"ln" : YYY",
"si" : [
{
"isTokenExpired" : true,
"oAuth" : {
"value" : "",
"permissions" : [ ]
},
"sIden" : {
"id" : "123",
"network" : 0
}
}
],
"tz" : "Etc/UTC"
}
但是pymongo在同一个查询中没有返回“si”数组,字段ln,fn是空的:
query = collection.find_one({'e.email': 'xxx@gmail.com', application: 'App1'})
print query
[{u'application': 'App1', u'tz': u'Etc/UTC', u'd': datetime.datetime(2013, 4, 30, 22, 52, 45, 916000), u'ln': u'', u'l': u'en_US', u'e': [{u'isValidated': True, u'email': u'xxx@gmail.com'}],u'_id': ObjectId('51804b3de4b092fd00c88d1b'), u'fn': u''}]
有什么问题?谢谢!
答案 0 :(得分:1)
在PyMongo中,您调用的是 findOne ,它只返回1个文档。而当您本地查询MongoDB时,您不会调用findOne,从而获得更多结果。从您的结果中可以看出,您正在使用 find()进行原生查询。 根据官方MongoDB documentation, findOne 和 find 之间存在差异。
findOne()
One document that satisfies the query specified as the argument
to this method. If the projection argument is specified, the
returned document contains only the projection fields, and the _id
field if you do not explicitly exclude the _id field.
find()
A cursor to the documents that match the query criteria.
If the projection argument is specified, the matching
documents contain only the projection fields, and the _id
field if you do not explicitly exclude the _id field.