2016-07-07 4 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] 
    }); 
} 

restaurants.restaurantId!= this.userIdの場合、upsertを使用して既存の顧客を更新したいが、その顧客を更新していない。新しい顧客を挿入し、restaurantId==this.userIdが正常に動作している顧客を更新する。upsertを使って配列内のオブジェクトを更新するには?

答えて

0

あなたは流星に真アップサート設定した場合、あなたは第三中括弧でアップサートを入れて持っている必要があります。

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

私はあなたのコードで4番目の括弧で "upsert:true"を渡すことができます。 –

+0

okk..Thanks .. btwアマゾンs3の流星で画像をアップロードする方法を知っていますか? –

関連する問題