質問

とい BlogPost モデルをゼロに多くの組込み Comment を提出する。い検索をしてPythonの返却 のみ Comment 物体マッチングマクエスペック?

例えば、 db.blog_posts.find({"comment.submitter": "some_name"}) を返すだけのリスト。

編集:例:

import pymongo

connection = pymongo.Connection()
db = connection['dvds']

db['dvds'].insert({'title': "The Hitchhikers Guide to the Galaxy",
                   'episodes': [{'title': "Episode 1", 'desc': "..."},
                                {'title': "Episode 2", 'desc': "..."},
                                {'title': "Episode 3", 'desc': "..."},
                                {'title': "Episode 4", 'desc': "..."},
                                {'title': "Episode 5", 'desc': "..."},
                                {'title': "Episode 6", 'desc': "..."}]})

episode = db['dvds'].find_one({'episodes.title': "Episode 1"}, 
                              fields=['episodes'])

この例では、 episode

{u'_id': ObjectId('...'),
 u'episodes': [{u'desc': u'...', u'title': u'Episode 1'},
               {u'desc': u'...', u'title': u'Episode 2'},
               {u'desc': u'...', u'title': u'Episode 3'},
               {u'desc': u'...', u'title': u'Episode 4'},
               {u'desc': u'...', u'title': u'Episode 5'},
               {u'desc': u'...', u'title': u'Episode 6'}]}

いきたいと思い:

{u'desc': u'...', u'title': u'Episode 1'}
役に立ちましたか?

解決

この同じ質問がMongo DB Googleグループページで尋ねられました。どうやらそれは現在不可能ですが、将来のために計画されています。

http://groups.google.com/group/mongodb-user/browse_thread/thread/4e6f5a0bac1abcck

他のヒント

あなたが望んだのはこれだと思います:

print db.dvds.aggregate([
  {"$unwind": "$episodes"}, # One document per episode
  {"$match": {"episodes.title": "Episode 1"} }, # Selects (filters)
  {"$group": {"_id": "$_id", # Put documents together again
              "episodes": {"$push": "$episodes"},
              "title": {"$first": "$title"} # Just take any title
             }
  },
])["result"]

出力(空白以外)は次のとおりです。

[ { u'episodes': [ { u'title': u'Episode 1',
                     u'desc': u'...'
                   }
                 ],
    u'_id': ObjectId('51542645a0c6dc4da77a65b6'),
    u'title': u'The Hitchhikers Guide to the Galaxy'
  }
]

あなたがから取り除きたい場合 u"_id", 、パイプラインを追加します。

  {"$project": {"_id": 0,
                "episodes": "$episodes",
                "title": "$title"}
               }

Mongodb JavaScriptシェルはで文書化されています http://docs.mongodb.org/manual/reference/method/

オブジェクトの特定のフィールドのみを取り戻したい場合は、使用できます

db.collection.find( { }, {fieldName:true});

一方、特定のフィールドを含むオブジェクトを探している場合、訴えることができます

db.collection.find( { fieldName : { $exists : true } } );

試合をよりシンプルです:

db['dvd'].find_one( {'episodes.title': "Episode 1"},{'episodes.title': true} )

クエリ:

coll.find( criteria, fields );

う特定の分野からのオブジェクトです。E.g.:

coll.find( {}, {name:true} );

http://www.mongodb.org/display/DOCS/dbshell+Reference

見る DB.EVAL:

あなたは次のようなことをする必要があります:

episode = connection['dvds'].eval('function(title){
  var t = db.dvds.findOne({"episodes.title" : title},{episodes:true});
  if (!t) return null;
  for (var i in t.episodes) if (t.episodes[i].title == title) return t.episodes[i];
}', "Episode 1");

したがって、エピソードのフィルタリングはサーバー側にあります。

私も同じ問題に会いました。私のやり方は、集計関数を使用することです。最初にそれを解き、次にそれを一致させます。

db.dvds.aggregate([{$unwind:"$episodes"},{$match:{"episodes.title":"Episode 1"}}])

結果は次のようになります

{ "_id" : ObjectId("5a129c9e6944555b122c8511"),
   "title" : "The Hitchhikers Guide to the Galaxy",
   "episodes" : { "title" : "Episode 1", "desc" : "..." } }

完璧ではありませんが、Pythonで編集できます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top