2016-06-23 13 views
0

Coupen Sammlung Vor-UpdateMongoDB - Update-Objekte in der Seite eine verschachtelte Array in einer Sammlung (verschachtelte Aktualisierung)

{ 
    "_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" }, 
     ] 
    }] 
} 

Ich habe storeCode, offer name & coupen .So Ich brauche die status des entsprechenden aktualisieren Objekt auf alloted.

Für Ex: Wenn storeCode = LULUMALL, name = Angebot1 & coupen = adssd, also muss ich die status des entsprechenden Objekts allotted .Nach Update aktualisieren, The Collection sollte so sein. Coupen Sammlung Nach einem Update

{ 
    "_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" }, 
     ] 
    }] 
} 

Antwort

0

ich diese Daten in einer neuen Variablen nur kopiert und den Wert von status Feld allotted basierend ändern auf die Bedingung.

Hier Shopdaten sind die Eingangsdaten

var updateData = shopData; 

updateData die Kopie shopData ist.

updateCoupen.offers[index].coupens[randomNo].status = 'allotted'; 

index und randomNo wird unter der Bedingung erhalten basiert.

Coupen.update({ "storeCode": store }, { $set: { 'offers': updateData.offers } }, function(err, numAffected) { 
    console.log('numAffected', numAffected); 

}) 

Code Complete

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); 

     }) 

    } 
}