gpt4 book ai didi

arrays - 在 mongodb( native js 或 .NET 驱动程序)的子数组的 sub 中拉取一个元素

转载 作者:可可西里 更新时间:2023-11-01 10:44:34 25 4
gpt4 key购买 nike

我在 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?

最佳答案

您将使用 $pull使用 update 操作符来执行此操作,使用 $位置运算符,用于识别您定位的 services 元素:

db.collection.update(
{ "_id" : ObjectId("505fd43fdbed3dd93f0ae088"), 'services.serviceName': 'Svc 1' },
{ $pull: { 'services.$.input': { "quantity" : 10, "note" : "quantity = 10" } } }
);

关于arrays - 在 mongodb( native js 或 .NET 驱动程序)的子数组的 sub 中拉取一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12558591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com