2015-06-25 36 views
5

我嘗試用貓鼬更新文檔,並且失敗。我可以成功地蒙戈直接執行的查詢是這樣的:

db.orders.update(
    { 
     orderId: 1014428, 
     'delivery.items.id': '5585d77c714a90fe0fc2fcb4' 
    }, 
    { 
     $inc: { 
      "delivery.items.$.quantity" : 1 
     } 
    } 
) 

當我嘗試運行與貓鼬以下update命令:

this.update(
     { 
      orderId: this.orderId , 
      "delivery.items.id": product.id 
     }, 
     { 
      $inc: { 
       "delivery.items.$.quantity" : 1 
      } 
     }, function (err, raw) { 
      if (err) { 
       console.log(err); 
      } 

      console.log('The raw response from Mongo was ', raw); 
     } 
    ); 

我看到下列錯誤:

{ [MongoError: cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})] 
    name: 'MongoError', 
    message: 'cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})', 
    index: 0, 
    code: 16837, 
    errmsg: 'cannot use the part (items of delivery.items.id) to traverse the element ({items: [ { quantity: 1, price: 6.9, name: "Manipulationstechniken", brand: null, id: "5585d77c714a90fe0fc2fcb4" } ]})' } 
The raw response from Mongo was { ok: 0, n: 0, nModified: 0 } 

我嘗試了很多東西。對此有何建議?

按照要求的模式:

var Order = new Schema({ 
    orderId: Number, 
    orderDate: String, 
    customerName: String, 
    state: Number, 
    delivery: { 
     items: {type: Array, default: []}, 
     state: { type: Number, default: 0 } 
    } 
}); 
+0

當我嘗試它時,它工作正常。 「this.update」調用中的「this」是什麼? – JohnnyHK

+0

「this」在模型函數內部,所以這是來自模型 - 在這種情況下Order。 –

回答

3

TL; DR:使用模型Order不是實例this的做更高級的查詢時:

Orders.update(
    { 
     orderId: this.orderId , 
     "delivery.items.id": product.id 
    }, 
    { 
     $inc: { 
      "delivery.items.$.quantity" : 1 
     } 
    }, function (err, raw) { 
     if (err) { 
      console.log(err); 
     } 

     console.log('The raw response from Mongo was ', raw); 
    } 
); 

說明:

映射Model.update()Document.update()之間的差異。

的使用模型,然後Model.update()將被用來和

Model.update(conditions, doc, options, callback) 

將映射到:

db.collection.update(query = conditions, update = doc, options) 

當使用實例,而不是您的通話Document.update()

Document.update(doc, options, callback) 

將映射到以下內容:

db.collection.update(query = {_id: _id}, update = doc, options) 
+0

偉大的,如果它的工作,不幸的是我碰到以下錯誤嘗試代碼: TypeError:對象#沒有方法'更新' 在model.Order.methods。 EventEmitter上的addProductToDelivery(/Users/kl3tte/development/st/models/order.js:149:15) 。 (/Users/kl3tte/development/st/routes/order.js:120:27) 在EventEmitter。 (/Users/kl3tte/development/st/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45) –

+0

然後在你的情況下訂單只是你的模式...你會得到一個模型與'貓鼬.model('Orders',Orders)' –

0

不知道這是否會有所幫助,但在this question的OP曾與貓鼬3.6.15類似的問題,並聲稱它是在22年3月8日

解決

編輯:在鏈接問題,OP了以下工作有關MongoDB

db.orchards.update(
({"orchardId": ObjectId("5391c137722b051908000000")}, 
{"trees" : { $elemMatch: {"name":"apple"}}}), 
{ $push: { "trees.$.fruits": ObjectId("54c542c9d9000000000") }}) 

但這在貓鼬不工作:

orchards.update(
({"orchardId": ObjectId.fromString(orchard.id)}, 
{"trees" : {$elemMatch: {"name": "apple"}}}), 
{$push: {"trees.$.fruits": ObjectId("54c542c9d9000000000") }},function(err, data){ ... 

在一個評論,他說,這個問題得到了解決切換到貓鼬22年3月8日

+0

我目前使用貓鼬4.0.5 –

+0

感謝您的建議,完成。但是,Tobias正在使用4.0。5,所以這可能不是他的解決方案 –