我在Mongo中有这些数据:
{
"_id": ObjectId("505fd43fdbed3dd93f0ae088"),
"categoryName": "Cat 1",
"services": [
{
"serviceName": "Svc 1",
"input": [
-------------------------------------------------------------------------
{ "quantity": 10, "note": "quantity = 10" },
-------------------------------------------------------------------------
{ "quantity": 20, "note": "quantity = 20" }
]
},
{
"serviceName": "Svc 2",
"input": [
{ "quantity": 30, "note": "quantity = 30" },
{ "quantity": 40, "note": "quantity = 40" }
]
}
]
}
现在我想拉出输入数组的元素{ "quantity" : 10, "note" : "quantity = 10" }
。我怎么能用Mongo做什么?
答案 0 :(得分:1)
您可以使用update
运算符和services
来执行此操作,使用$pull
位置运算符来标识您要定位的db.collection.update(
{ "_id" : ObjectId("505fd43fdbed3dd93f0ae088"), 'services.serviceName': 'Svc 1' },
{ $pull: { 'services.$.input': { "quantity" : 10, "note" : "quantity = 10" } } }
);
元素:
{{1}}