问题描述
假设我有一个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查询仅返回嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!