我有一个mongo文档,在这个文档中我需要删除所有缺少id的line_items元素。问题是carrier_orders本身是一个数组,line_items也是一个数组。以下是文档的结构:
{
"carrierOrder": [
{
"id": 1636354,
"costs": {
"totalAmount": 110,
"lineItem": [
{
"amount": 110,
"deleted": false,
"id": 3464888,
"price": 55,
"qty": 2,
"code": {
"id": 135431,
"key": "1600",
"value": "Freight - flat"
}
}
]
},
"deleted": true,
"carrierPO": ""
},
{
"id": 1668540,
"costs": {
"totalAmount": 110,
"lineItem": [
{
"amount": 110,
"deleted": false,
"id": 3527307,
"price": 55,
"qty": 2,
"code": {
"id": 135431,
"key": "1600",
"value": "Freight - flat"
}
},
{
"amount": 110,
"deleted": false,
"price": 110,
"qty": 1,
"code": {
"id": 135431,
"key": "1600",
"value": "Freight - flat"
}
}
]
},
"deleted": false,
"carrierPO": ""
}
],
"id": 3949038,
"customId": "31428-26396"
}我尝试了许多查询,并使用下面的查询关闭
db.shipmentFormFieldData.update({"value.carrier_orders.$[].costs.line_items.$[].id":{$exists: false}},{$unset:{"value.carrier_orders.$[].costs.line_items":""}})我想删除所有缺少id字段的line_items
发布于 2019-10-11 01:27:18
下面的查询可以得到预期的输出:
db.shipmentFormFieldData.updateMany({},
{
$pull:{
"carrierOrder.$[].costs.lineItem":{
"id":null
}
}
})https://stackoverflow.com/questions/58326800
复制相似问题