I have a nested array, I have to remove the one object , based upon the condition using aggregation

这是我从MongoDB得到的JSON

{
  "_id":  "633d275ceb34a28755974032",
  "name": "free",
  "products": [
    {
      "title": "Product 1",
      "phone": [
        {
          "title": "Best Phone 1 ",
          "video": {
            "Location": "https://video.mp4"
          },
          "freePreview": true
        }
      ]
    },
    {
      "title": "Product 2",
      "phone": [
        {
          "title": "Best Phone 2",
          "video": {
            "Location": "https://video.mp4"
          },
          "freePreview": false
        }
      ]
    }
    
  ] 
}

但我需要像这样的数据

{
  "_id":  "633d275ceb34a28755974032",
  "name": "free",
  "products": [
    {
      "title": "Product 1",
      "phone": [
        {
          "title": "Best Phone 1 ",
          "video": {
            "Location": "https://video.mp4"
          },
          "freePreview": true
        }
      ]
    },
    {
      "title": "Product 2",
      "phone": [
        {
          "title": "Best Phone 2",
          "freePreview": false
        }
      ]
    }
    
  ] 
}

In this data "video object" is removed inside the phone array , because of freePreview is false,

根据免费预览条件,帮我移除视频对象

推荐答案

您需要使用Double $map运算符来迭代数组数组,并使用$cond$$REMOVE来有条件地删除值:

db.collection.aggregate([
    {
        $addFields: {
            products: {
                $map: {
                    input: "$products",
                    as: "p",
                    in: {
                        title: "$$p.title",
                        phone: {
                            $map: {
                                input: "$$p.phone",
                                in: {
                                    title: "$$this.title",
                                    freePreview: "$$this.freePreview",
                                    video: {
                                        $cond: {
                                            if: {
                                                $eq: [
                                                    false,
                                                    "$$this.freePreview"
                                                ]
                                            },
                                            then: "$$REMOVE",
                                            else: "$$this.video"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
])

Mongo Playground

Here's个类似的例子.

Node.js相关问答推荐

promise 所有映射或反对

Twilio-获取呼叫录音不起作用的请求

Jest由于UUID而无法解析测试,即使在Jest中启用ESModule支持后也是如此

使用xml-crypto时出现NodeJS XPath解析错误

NPM:无法导入本码模块

Inno Setup如何在现有文本文件中追加新内容

在 puppeteer 中从 pdf 中删除 about:blank 和 date-time

Mongoose post中间件触发deleteMany

如何配置 Nginx React 和 Express

每个数组值在 mongodb 中查找一个文档

使用react 组件加载特定 DIV 而无需重新加载整个页面

如何使用填充在mongoose 上保存新数据

无法关闭 node.js 中的mongoose 连接

将环境变量从 GitHub 操作传递到 json

在 `DataFrame` 上使用用户定义的可链接函数抽象出 Polars 表达式

如何使用 superagent/supertest 链接 http 调用?

如何在不全局安装的情况下在 Node REPL 中要求 node 模块?

node 应用程序是否有任何独立的 gui 模块

`return function *(){...}` 是什么意思?

CORS 错误:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段授权