2015-06-25 2 views
5

मैं मोंगोज़ के साथ एक दस्तावेज़ को अपडेट करने का प्रयास करता हूं और यह विफल रहता है। क्वेरी मैं सफल मोंगो में सीधे निष्पादित कर सकते हैं है की तरह:

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

जब मैं नेवला के साथ निम्न अद्यतन आदेश चलाने का प्रयास:

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' कॉल में' यह' क्या है? – JohnnyHK

+0

"यह" मॉडल फ़ंक्शन के अंदर है, इसलिए यह मॉडल से है - इस मामले में ऑर्डर। –

उत्तर

3

टी एल नेवला करने के लिए; डॉ: एक उदाहरण this के बजाय अपने मॉडल Order का उपयोग करते हैं और अधिक उन्नत प्रश्नों कर:

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

यदि यह काम करता है तो बहुत अच्छा होगा, दुर्भाग्यवश मैं कोड को आजमाने में त्रुटि का पालन करने के लिए चला गया: टाइप एरर: ऑब्जेक्ट # में मॉडल.ऑर्डर.माइट्स पर कोई विधि नहीं है। 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

फिर आपके मामले में ऑर्डर केवल आपकी स्कीमा है ... आपको 'मोंगोज़' वाला मॉडल मिलेगा । मॉडल ('ऑर्डर', ऑर्डर) ' –

0

अगर यह मदद करता है पता नहीं है, लेकिन this question में ओपी नेवला 3.6.15 के साथ एक समान मुद्दा था, और दावा किया कि यह 3.8.22

में हल किया गया था

संपादित करें: लिंक किए गए सवाल में, ओपी अगले कार्य 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){ ... 

एक टिप्पणी में उन्होंने कहा कि इस मुद्दे को हल किया गया था स्विचिंग 3.8.22

+0

मैं वर्तमान में सलाह के लिए धन्यवाद, mongoose 4.0.5 –

+0

धन्यवाद। हालांकि, टोबीस 4.0 का उपयोग कर रहा है।5, तो यह उनके लिए समाधान नहीं हो सकता है –

संबंधित मुद्दे