16

假设我有一个BlogPost包含零对多嵌入Comment文档的模型。我可以查询并让 MongoDB 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'}
4

6 回答 6

8

我想你想要的是这样的:

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"}
               }
于 2013-03-28T11:25:44.887 回答
6

Mongo DB Google Groups 页面上也提出了同样的问题。显然它目前不可能,但它计划在未来。

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

于 2010-02-15T22:34:20.553 回答
3

mongodb javascript shell 记录在http://docs.mongodb.org/manual/reference/method/

如果你想只取回一个对象的特定字段,你可以使用

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

另一方面,如果您正在寻找包含特定字段的对象,您可以起诉

db.collection.find( { fieldName : { $exists : true } } );
于 2010-02-15T20:14:00.013 回答
1

搭配更简单:

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

询问:

coll.find( criteria, fields );

仅从对象中获取特定字段。例如:

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

http://www.mongodb.org/display/DOCS/dbshel​​l+参考

于 2010-05-10T04:11:44.393 回答
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对其进行编辑。

于 2017-11-20T10:31:41.887 回答
0

看看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");

因此对剧集的过滤将在服务器端进行。

于 2010-02-15T22:59:39.977 回答