2017-04-04 4 views
1

MongoDBでは、サブフィールドに対してprojectionsを実行できます。たとえば、db.collection.find({},{"field.subfield":1, "_id":0})はすべてのドキュメントを返し、指定されたサブフィールドのみを表示し、他のフィールドを出力から非表示にします。サブフィールド投影のPyMongoサポート

PyMongoはsupport投影ですが、サブフィールドでは機能しないようです。コード例を以下に示します。

私の質問は次のとおりです:PyMongoはサブフィールド予測をサポートしていますか?はいの場合、正しい構文は何ですか?私にとって

+0

正しい構文です。文書の例を提供してください – Dmitry

答えて

1

作品:

from pymongo import MongoClient 
client = MongoClient() 
client.db.collection.drop() 
client.db.collection.insert_one({ 
    'field1': 1, 
    'field2': { 
     'subfield': 'sub', 
     'othersubfield' : 'othersub' 
    } 
}) 

print(list(client.db.collection.find(projection=['field1','field2.subfield']))) 

このプリントは:

[{u'field2': {u'subfield': u'sub'}, u'field1': 1, u'_id': ObjectId('58e3d11dca1ce936cf6498c4')}] 

あなたはそれが投影から省略されているので、 "othersubfield"、含まないであることを確認します。

関連する問題