2013-04-30 33 views
0

我遇到以下問題。 Pymongo返回的字段比它應該少。pymongo返回較少的字段

這裏是我的查詢: db.users.findOne({'e.email': '[email protected]', application: 'App1'})

直接從蒙戈DB我得到: { "_id" : ObjectId("51803128e4b092fd00c8899b"), "application": "App1", "d" : ISODate("2013-04-30T21:01:28.084Z"), "e" : [ { "email" : "[email protected]", "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': '[email protected]', 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'[email protected]'}],u'_id': ObjectId('51804b3de4b092fd00c88d1b'), u'fn': u''}]

什麼問題?謝謝!

+0

您可以檢查是否有超過1個文件滿足此查詢條件。如果在兩個結果中觀察到_id字段是不同的。 'd'參數也不同。所以他們很可能都沒有拿到相同的文件。 – 2013-04-30 23:44:14

+0

只有一個這樣的文檔。一個應用程序不能有兩個完全相同的電子郵件。但我也試過find而不是find_one,並檢查了只有一個。 – 2013-05-01 00:09:45

+0

即使「ln」字段也不匹配。你能創建一些虛擬文件並測試嗎? – 2013-05-01 00:16:24

回答

1

在PyMongo中你打電話findOne,它將只返回1個文件。而當你在本地查詢MongoDB時,你不會調用findOne,從而獲得更多結果。從您的結果中可以看出,您正在使用find()進行本機查詢。 這是findOne找到按照官方MongoDB documentation之間的差異。

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. 
+0

對不起尼朋,你回答了不同的問題。這是我的錯誤。我講的是錯過的領域,而不是文件。我會編輯我的問題。 – 2013-04-30 23:38:07