2013-06-25 98 views
2

我想一個蒙戈集合,其中有可能是同一集合內的多個比賽中更新多個子文檔。不幸的是,它只是更新每個文檔中匹配的第一個子文檔。多子文檔更新

下面是代碼:

var mongoose = require('mongoose'); 

mongoose.connect('mongodb://127.0.0.1/touchbits'); 

var PartsSchema = new mongoose.Schema({ 
     type: String 
     , partNbr: Number 
    }); 

var ProductSchema = new mongoose.Schema({ 
     sku: { type: String, unique: true } 
     , parts: [PartsSchema] 
    }); 

Product = mongoose.model('Product', ProductSchema); 

var cigars = new Product({ 
     sku: 'cigar123', 
     parts: [{type: 'tobacco', partNbr: 4}, 
       {type: 'rolling paper', partNbr: 8}, 
       {type: 'tobacco', partNbr: 4}] 
    }); 
var cigarillo = new Product({ 
     sku: 'cigarillo456', 
     parts: [{type: 'tobacco', partNbr: 4}, 
       {type: 'crush paper', partNbr: 12}] 
    }); 

cigars.save(function(err, product1) { 
     if(err) { console.log("err1: " + err); return err; } 
     console.log("saved: " + product1); 
     cigarillo.save(function(err, product2) { 
       if(err){ console.log("err2: " + err); return err; } 
       console.log("saved: " + product2); 
       Product.update({ "parts.type": 'tobacco' }, 
           { $set: { "parts.$.partNbr": 5 } }, 
           { multi: true }, 
           function(err, numAffected) { 
            if (err) { console.log("err3: " + err); return err; } 
            console.log("records updated: " + numAffected); 
            Product.find({}, function(err, docs) { 
              if (err) { console.log("err4: " + err); return err; } 
              console.log("updated data: " + docs); 
              Product.remove(function(err) { 
                if (err) { console.log("err5: " + err); return err; } 
                process.exit(); 
               }); 
             }); 
           }); 
      }); 
    }); 

然後當我運行它,我得到如下結果:

saved: { __v: 0, 
    sku: 'cigar123', 
    _id: 51c9229b7fe80ef71e000002, 
    parts: 
    [ { type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000005 }, 
    { type: 'rolling paper', 
     partNbr: 8, 
     _id: 51c9229b7fe80ef71e000004 }, 
    { type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000003 } ] } 
saved: { __v: 0, 
    sku: 'cigarillo456', 
    _id: 51c9229b7fe80ef71e000006, 
    parts: 
    [ { type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000008 }, 
    { type: 'crush paper', 
     partNbr: 12, 
     _id: 51c9229b7fe80ef71e000007 } ] } 
records updated: 2 
updated data: { sku: 'cigar123', 
    _id: 51c9229b7fe80ef71e000002, 
    __v: 0, 
    parts: 
    [ { type: 'tobacco', partNbr: 5, _id: 51c9229b7fe80ef71e000005 }, 
    { type: 'rolling paper', 
     partNbr: 8, 
     _id: 51c9229b7fe80ef71e000004 }, 
    { type: 'tobacco', partNbr: 4, _id: 51c9229b7fe80ef71e000003 } ] },{ sku: 'cigarillo456', 
    _id: 51c9229b7fe80ef71e000006, 
    __v: 0, 
    parts: 
    [ { type: 'tobacco', partNbr: 5, _id: 51c9229b7fe80ef71e000008 }, 
    { type: 'crush paper', 
     partNbr: 12, 
     _id: 51c9229b7fe80ef71e000007 } ] } 

注意,「雪茄」文檔中的第二個「菸草」錄製靜止有PartNbr 4而不是5.有誰知道如何更改更新以更新數組中與查詢條件匹配的所有子文檔?

回答

5

內這樣一個請求尚未實現更新多個子文件的功能。

它目前請求在https://jira.mongodb.org/browse/SERVER-1243

+0

謝謝你的鏈接。 IYO是繼續更新文檔直到沒有更新記錄更新的最佳解決方法? – frank

+0

如果你不能改變你的模式結構或不想從Mongo中提取整個文檔並更新你的應用程序,然後推回去;那麼這將工作。 – daveh