0

我有以下问题。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''}]

什么问题?谢谢!

4

1 回答 1

1

在 PyMongo 中,您正在调用findOne,它只会返回 1 个文档。而当您在本地查询 MongoDB 时,您不会调用 findOne 并因此获得更多结果。从您的结果可以看出,您正在使用find()本机进行查询。根据官方 MongoDB文档,这是findOnefind之间的区别。

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.
于 2013-04-30T23:30:25.837 回答