【问题标题】:how to remove empty document if it not matched in mongodb?如果在 mongodb 中不匹配,如何删除空文档?
【发布时间】:2019-07-31 18:09:54
【问题描述】:

如果 mongoDB 中的条件不匹配,请告诉我如何删除空文档

我在 shell 上试​​过这个查询

db.address.aggregate([
    { "$project": {
            "applications": {
                "$filter": {
                    "input": "$applications",
                    "as": "applications",
                    "cond": {
                        "$and": [
                            { "$eq": ["$$applications.name", "DSB"] },
                            { "$in": ["$$applications.code", ["122018"]] }
                        ]
                    }
                }
            }
        }}
])

我的收藏

[
  {
    "name": "test",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "test1",
    "applications": [
      {
        "name": "HELLO",
        "code": [
          "135001",
          "122017"
        ]
      }
    ]
  },
  {
    "name": "test2",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "test3",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "test4",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "test5",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "tes3",
    "applications": [
      {
        "code": [
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "test22",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "DSB"
      }
    ]
  },
  {
    "name": "test89",
    "applications": [
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "DSB"
      },
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  },
  {
    "name": "test89",
    "applications": [
      {
        "code": [
          "135001",
          "122018"
        ],
        "name": "DSB"
      },
      {
        "code": [
          "135001",
          "122017"
        ],
        "name": "HELLO"
      }
    ]
  }
]

预期输出

   [
  {
    "name": "test89",
    "applications": [
      {
        "code": [
          "135001",
          "122018"
        ],
        "name": "DSB"
      }
    ]
  }
]

出去

{ "_id" : ObjectId("5d4143269d7b3f8883605a1e"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a1f"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a20"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a21"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a22"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a23"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a24"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a25"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a26"), "applications" : [ ] }
{ "_id" : ObjectId("5d4143269d7b3f8883605a27"), "applications" : [ ] }

请说明为什么我没有得到预期的输出??

我想过滤我的集合,其中包含 application 名称“DSB”和代码 122018

查看我的预期输出,其中只有一个满足我条件的文档,名称为 DSB,代码为“122018”。

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query


    【解决方案1】:

    您需要使用unwind 并且您的$in 条件中存在位置问题

    db.address.aggregate([
        {
            "$project": {
                "applications": {
                    "$filter": {
                        "input": "$applications",
                        "as": "applications",
                        "cond":  {
                            "$and": [
                                { "$in": ['122018',"$$applications.code"] },
                                { "$eq": ["$$applications.name", "DSB"] },
                            ]
                        }
                    }
                },
                name:true
            }
        },
        { $unwind: { path: "$applications", preserveNullAndEmptyArrays: false } }
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      相关资源
      最近更新 更多