【问题标题】:Is it possible to query for a specific object in an array with MongoDB?是否可以使用 MongoDB 查询数组中的特定对象?
【发布时间】:2011-09-03 16:59:06
【问题描述】:

假设你有一个这样的博客集

{"title": "Post Title", "body": "Post Body", "comments": [{"user": "user1", "comment": "comment from user1"}, {"user": "user2", "comment": "comment from user2"}]} {"title": "Post Title 2", "body": "Post Body 2", "comments": [{"user": "user1", "comment": "comment from user1"}]}

如何查询用户的 cmets?

此查询有效,但返回来自 user2 和 user1 的 cmets。 db.posts.find({"comments.user": "user1"})

有没有办法只从 user1 获取 cmets?

【问题讨论】:

标签: mongodb


【解决方案1】:

我认为没有办法,因为 Mongo 只是在找到匹配项时返回整个文档,您可以使用 Map/Reduce 或服务器处理来做到这一点

【讨论】:

    【解决方案2】:

    此功能请求可能会对您有所帮助...何时实施(如果没有延迟,则需要几个月)。

    https://jira.mongodb.org/browse/SERVER-828

    【讨论】:

    • 所以我猜他们错过了大约 7 周前不到一周的时间。希望这会在某天发布,但在那之前我想我会坚持使用 jeff 建议的 Map/Reduce。
    猜你喜欢
    • 2014-01-08
    • 2019-02-24
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 2021-09-24
    相关资源
    最近更新 更多