【发布时间】: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