更新前的Coupen系列
{
"_id": ObjectId("571501b7cbd85fe5ce5a749c"),
"storeCode": "LULUMALL",
"offers": [{
"name": "offer1",
"coupens": [
{ "coupen": "adssd", "status": "open" },
{ "coupen": "sdsafa", "status": "open" },
{ "coupen": "wewerw", "status": "open" },
{ "coupen": "cvvdf", "status": "open" },
{ "coupen": "dfdfadf", "status": "open" },
{ "coupen": "dfdfd", "status": "open" },
{ "coupen": "dfdf", "status": "open" },
{ "coupen": "dfsdf", "status": "open" }
}
]
},
{
"name": "offer2",
"coupens": [
{ "coupen": "dffddd", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "sdfsdf", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "sfsdfd", "status": "open" },
{ "coupen": "dffdfd", "status": "open" },
{ "coupen": "sdfsdf", "status": "open" },
{ "coupen": "dfdfdd", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "dfsdff", "status": "open" },
]
}]
}
我有storeCode
,offer name
和coupen
。所以我需要将相应对象的status
更新为alloted
。
对于Ex:如果storeCode
= LULUMALL,name
= offer1& coupen
= adssd,所以我需要将相应对象的status
更新为allotted
。更新后,The Collection应该是这样的。
更新后的Coupen集合
{
"_id": ObjectId("571501b7cbd85fe5ce5a749c"),
"storeCode": "LULUMALL",
"offers": [{
"name": "offer1",
"coupens": [
{ "coupen": "adssd", "status": "allotted" },
{ "coupen": "sdsafa", "status": "open" },
{ "coupen": "wewerw", "status": "open" },
{ "coupen": "cvvdf", "status": "open" },
{ "coupen": "dfdfadf", "status": "open" },
{ "coupen": "dfdfd", "status": "open" },
{ "coupen": "dfdf", "status": "open" },
{ "coupen": "dfsdf", "status": "open" }
}
]
},
{
"name": "offer2",
"coupens": [
{ "coupen": "dffddd", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "sdfsdf", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "sfsdfd", "status": "open" },
{ "coupen": "dffdfd", "status": "open" },
{ "coupen": "sdfsdf", "status": "open" },
{ "coupen": "dfdfdd", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "dfsdff", "status": "open" },
]
}]
}
答案 0 :(得分:0)
我刚将这些数据复制到新变量中,并根据条件将status
字段的值更改为allotted
。
这里shopData是输入数据
var updateData = shopData;
updateData
是shopData
的副本。
updateCoupen.offers[index].coupens[randomNo].status = 'allotted';
index
和randomNo
将根据条件得出。
Coupen.update({ "storeCode": store }, { $set: { 'offers': updateData.offers } }, function(err, numAffected) {
console.log('numAffected', numAffected);
})
完整代码
var store = 'LULUMALL';
var name = 'offer1';
var coupen = 'adssd';
var shopData = {
"_id": ObjectId("571501b7cbd85fe5ce5a749c"),
"storeCode": "LULUMALL",
"offers": [{
"name": "offer1",
"coupens": [
{ "coupen": "adssd", "status": "open" },
{ "coupen": "sdsafa", "status": "open" },
{ "coupen": "wewerw", "status": "open" },
{ "coupen": "cvvdf", "status": "open" },
{ "coupen": "dfdfadf", "status": "open" },
{ "coupen": "dfdfd", "status": "open" },
{ "coupen": "dfdf", "status": "open" },
{ "coupen": "dfsdf", "status": "open" }
}
]
},
{
"name": "offer2",
"coupens": [
{ "coupen": "dffddd", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "sdfsdf", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "sfsdfd", "status": "open" },
{ "coupen": "dffdfd", "status": "open" },
{ "coupen": "sdfsdf", "status": "open" },
{ "coupen": "dfdfdd", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "dfdfdf", "status": "open" },
{ "coupen": "dfsdff", "status": "open" },
]
}]
}
for (var i = 0; i < shopData.offers.length; i++) {
if (shopData.offers[i].name === offer) {
var coupen = [];
var index = i;
for (var j = 0; j < shopData.offers[i].coupens.length; j++) {
if (shopData.offers[i].coupens[j].status === "open") {
coupen.push(shopData.offers[i].coupens[j]);
}
}
console.log('Available Coupens', coupen);
var randomNo = Math.floor(Math.random() * coupen.length);
var rand = coupen[randomNo];
var updateCoupen = shopData;
updateCoupen.offers[index].coupens[randomNo].status = 'allotted';
Coupen.update({ "storeCode": store }, { $set: { 'offers': updateCoupen.offers } }, function(err, numAffected) {
console.log('numAffected', numAffected);
})
}
}