当前位置:  开发笔记 > 编程语言 > 正文

MongoDB查询只返回嵌入文档

如何解决《MongoDB查询只返回嵌入文档》经验,为你挑选了2个好方法。

假设我有一个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'}

H.D... 7

我想你想要的是:

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"}
               }


Stephen Curr.. 6

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

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



1> H.D...:

我想你想要的是:

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"}
               }



2> Stephen Curr..:

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

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

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有