MongoDB:使用嵌套数组过滤的 find 和 findOne [英] MongoDB: find and findOne with nested array filtering

查看:42
本文介绍了MongoDB:使用嵌套数组过滤的 find 和 findOne的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果证明这个小任务比我想象的要难.

This little task turned out to be harder than I thought.

考虑以下非常简单的 Posts 集合.假设我想显示所有帖子,只显示没有删除的评论.

Consider the following very simple Posts collection. Suppose I want to display all the posts, coupled with only the comments that were not deleted.

I.E.从comments数组中过滤掉已删除的评论.

I.E. filter out the deleted comments from the comments array.

由于我每个帖子有 100 条已删除的评论,有没有办法做到这一点?

Since I have 100s of deleted comments per post, is there a way to do this server side?

集合:

{
  "author": {},
  "message": "This is post1",
  "comments": [
    {
      "message": "Im number 1!!!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "YOU MOTHERF****R",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "tHIS IS GREAT!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "I can type better than you guys",
      "state": {
        "deleted": false
      }
    }
  ]
},
{
  "author": {},
  "message": "This is post 2",
  "comments": [
    {
      "message": "This is bulls**t",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "YOU MOTHERF****R",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "I hate u!",
      "state": {
        "deleted": true
      }
    },
    {
      "message": "I wanna have your children",
      "state": {
        "deleted": false
      }
    }
  ]
}

推荐答案

我会TL;DR,因为结果证明这是一个地狱般的旅程.我试过 $elemMatch,我试过 $redact(还有 $$CURRENT 和 $$ROOT),我试过 $map,我试过聚合框架,我试过$project.

I'll TL;DR since this turned out to be a hell of a ride. I've tried $elemMatch, I've tried $redact (also with $$CURRENT and $$ROOT), I've tried $map, I've tried the aggregation framework, I've tried $project.

您可以在这里阅读所有相关信息:https://www.devsbedevin.net/mongodb-find-findone-with-nested-array-filtering-finally/

You can read all about it here: https://www.devsbedevin.net/mongodb-find-findone-with-nested-array-filtering-finally/

解决方案似乎是使用聚合框架来过滤数组并用结果覆盖comments 属性.这比听起来简单:

The solution seems to be to use the aggregation framework to filter the array and override the comments property with the results. This is simpler than it sounds:

db.getCollection('posts').aggregate(
    {$match: {"author.id": authorId}},
    {$addFields : {"comments":{$filter:{ // We override the existing field!
        input: "$comments",
        as: "comment",
        cond: {$eq: ["$$comment.state.deleted", false]}
    }}}}
);

结果:

{
  "author": {},
  "message": "This is post1",
  "comments": [
    {
      "message": "Im number 1!!!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "tHIS IS GREAT!",
      "state": {
        "deleted": false
      }
    },
    {
      "message": "I can type better than you guys",
      "state": {
        "deleted": false
      }
    }
  ]
},
{
  "author": {},
  "message": "This is post 2",
  "comments": [
    {
      "message": "I wanna have your children",
      "state": {
        "deleted": false
      }
    }
  ]
}

这篇关于MongoDB:使用嵌套数组过滤的 find 和 findOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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