產品和變型價格均products.Variants.Price
的MongoDB中的foreach DOC發現分鐘子集和更新
搜索最小值,其中規模較小,15%的更新
{
"_id" : 23,
"name" : "Polo Shirt",
"Variants" : [
{
"size" : "Large",
"Price" : 82.42
},
{
"size" : "Medium",
"Price" : 20.82 // this should get increased by 15%
},
{
"size" : "Small",
"Price" : 42.29
}
]
},
{
"_id" : 24,
"name" : "Polo Shirt 2",
"Variants" : [
{
"size" : "Large",
"Price" : 182.42
},
{
"size" : "Medium",
"Price" : 120.82 // this should get increased by 15%
},
{
"size" : "Small",
"Price" : 142.29
}
]
}
我開始是這樣的。不知道這是正確的開始
db.products.find().forEach(function(product){
var myArr = product.Variants;
print(myArr.min());
});
如果你需要在你的變體中找到min,你爲什麼要迭代集合中的documetns? –
我需要更新最小的一個。 – HaBo
集合中所有文檔的最小值還是文檔中所有變體的最小值? –