我的JSON对象如下所示,我想更新用户1的一个产品详细信息 通过使用用户ID和产品ID
{
"_id": {
"$oid": "5b544ee53379672e184c9aa2"
},
"user_firstname": "user 1",
"user_lastname": "test123",
"products": [
{
"_id": {
"$oid": "5b5615741026382bf0b784a6"
},
"product_name": "product 1",
"product_price": {
"$numberInt": "25"
},
},
{
"_id": {
"$oid": "5b5615ba1026382bf0b784a7"
},
"product_name": "product 2",
"product_price": {
"$numberInt": "25"
},
},
]
}{
"_id": {
"$oid": "5b574e38b714e420683ae839"
},
"user_firstname": "user2",
"user_lastname": "user last name",
"products": [
{
"_id": {
"$oid": "5b574e7fb714e420683ae83b"
},
"product_name": "product1",
"product_price": {
"$numberInt": "10"
},
},
],
}
这里有两个用户,他们有一些产品。现在,我想通过找到特定用户的ID来更新其产品详细信息。在上面的示例中,我要更新ID为“ $ oid”的用户的产品详细信息“ $ oid”:“ 5b5615ba1026382bf0b784a7” :“ 5b544ee53379672e184c9aa2”
我将从用户那里得到的详细信息
updateproducts = {
product_id : "5b5615ba1026382bf0b784a7"
product_name : "updated product"
product_price :"250"
userID : "5b544ee53379672e184c9aa2"
}
这是我的功能
module.exports.updateProducts = function(updateproducts, callback) {
var id = updateproducts.product_id;
sellers.update({ "_id": ObjectId(id) }, { '$set': updateproducts },
callback);
}