MongoDB查询仅返回嵌入式文档 [英] MongoDB query to return only embedded document

查看:220
本文介绍了MongoDB查询仅返回嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个BlogPost模型,其中包含零对多嵌入的Comment文档.我可以查询MongoDB并返回仅 Comment个与我的查询规范匹配的对象吗?

assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i query for and have MongoDB return only Comment objects matching my query spec?

例如,db.blog_posts.find({"comment.submitter": "some_name"})仅返回评论列表.

eg, db.blog_posts.find({"comment.submitter": "some_name"}) returns only a list of comments.

一个例子:

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网上论坛页面上也问过同样的问题.显然,目前尚不可能,但已计划在未来进行.

This same question was asked over on the Mongo DB Google Groups page. Apparently its not currently possible but it is planned for the future.

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

这篇关于MongoDB查询仅返回嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆