2016-07-07 62 views
0
var newBillObject = { 
    billId: Random.id(), 
    billAmount: bill.billAmount, 
    billDate: new Date() 
}; 
var pointAdded = Math.round((bill.pointPercent/100) * bill.billAmount); 
var data = Customers.findOne({ "phone": bill.phone }); 

if (data) { 
    var restaurantInfo = { 
     restaurantId: this.userId, 
     points: pointAdded, 
     createdAt: new Date(), 
     lastVisited: new Date() 
    }; 

    Customers.update({ "restaurants.restaurantId": this.userId }, { 
      $push: { customerBills: newBillObject }, 
      $set: { "restaurants.$.lastVisited": new Date() }, 
      $inc: { "restaurants.$.points": pointAdded }  
     }, { 
      $setOnInsert: { 
       "restaurants.$.createdAt": new Date() 
      } 
     },{ upsert: true } 
    ); 
} else {   
    // return 
    // Customers.update({"restaurants.restaurantId":this.userId ,'phone':bill.phone}, { 
    var restaurantInfo = { 
     restaurantId: this.userId, 
     points: pointAdded, 
     createdAt: new Date(), 
     lastVisited: new Date() 
    } 

    return Customers.insert({ 
     name: bill.name, 
     phone: bill.phone,  
     code: bill.code, 
     restaurants: [restaurantInfo],   
     customerBills: [newBillObject] 
    }); 
} 

我想使用upsert更新現有客戶,如果restaurants.restaurantId!= this.userId但它不更新該客戶。插入新客戶並更新restaurantId==this.userId工作正常的客戶。如何使用upsert更新數組中的對象?

回答

0

當您設置UPSERT流星真實的,你必須得把UPSERT第三大括號:像

Customers.update({ 
    "restaurants.restaurantId": this.userId 
}, { 
    $push: { 
     customerBills: newBillObject 
    }, 
    $set: { 
     "restaurants.$.lastVisited": new Date() 
    }, 
    $inc: { 
     "restaurants.$.points": pointAdded 
    }, 
    $setOnInsert: { 
     "restaurants.$.createdAt": new Date() 
    } 
}, { 
    upsert: true 
}); 
+0

我也做了相同的。 –

+0

我可以在你的代碼中看到你在第四個括號中傳遞「upsert:true」。 –

+0

okk ..感謝.. btw你知道任何方法如何上傳流星在Amazon S3上的圖像? –

相關問題