2015-06-25 28 views
5

Cerco di aggiornare un documento con mangusta e non riesce. La query che posso eseguire con successo direttamente in Mongo è come:

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

Quando provo ad eseguire il seguente comando update con mangusta:

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

vedo il seguente errore:

{ [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 } 

Ho provato così tante cose. Qualche consiglio su questo?

Come richiesto lo schema:

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

Ha funzionato bene quando l'ho provato. Cos'e' questo? Nella chiamata 'this.update'? – JohnnyHK

+0

"questo" è all'interno di una funzione modello, quindi questo è dal modello - in questo caso Ordine. –

risposta

3

TL; DR: utilizzare il modello di Order invece di un'istanza this quando fare query più avanzate:

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

Spiegazione:

Differenze di mappatura tra Model.update() e Document.update().

Il utilizzando il modello, verrà utilizzato Model.update() e

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

verrà mappata:

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

Quando si utilizza un'istanza invece la vostra vocazione Document.update() e

Document.update(doc, options, callback) 

verrà mappato al seguente:

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

Sarebbe bello se funzionasse, sfortunatamente mi sono imbattuto nel seguente errore provando il codice: TypeError: Object # non ha alcun metodo 'update' in model.Order.methods. addProductToDelivery (/Users/kl3tte/development/st/models/order.js:149:15) su EventEmitter. (/Users/kl3tte/development/st/routes/order.js:120:27) su EventEmitter. (/Users/kl3tte/development/st/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45) –

+0

Quindi nel tuo caso gli ordini sono solo il tuo schema ... Otterrai un modello con 'mangusta .model ('Ordini', Ordini) ' –

0

Non so se questo aiuta, ma in this question PO ha avuto un problema simile con mangusta 3.6.15, e ha sostenuto è stato risolto nel 3.8.22

EDIT: nella questione collegata, il PO ha avuto il seguente lavoro sulla mongodb

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

Ma questo non funziona in mangusta:

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

In un commento, ha detto che il problema è stato risolto di commutazione per mangusta 3.8.22

+0

Attualmente uso mangusta 4.0.5 –

+0

Grazie per il consiglio, fatto. Tuttavia, Tobias utilizza 4.0.5, quindi questa potrebbe non essere la soluzione per lui –

Problemi correlati