【问题标题】:Mongoose / MongoDB. Cannot save array of strings猫鼬/MongoDB。无法保存字符串数组
【发布时间】:2014-11-07 15:26:18
【问题描述】:

更新:

终于找到了。下面的代码是完全有效的。只是在另一个模块中对 Layouts.blocks 进行了破坏性更新...


这里发生了一些非常奇怪的事情。

这是Layouts 架构的相关部分

blocks: [{
   full: {type: Boolean},
  fields: [{type: String}]
}]

和.pre保存另一个模型的中间件代码

Map.pre('save', function(next){
  var that = this;
  var Layouts = connection.model('layouts');
  var openFields = ['one', 'two'];
  Layouts.find({_company: that._company, object: that._id, default: true}, function(err, layouts){
    if (err) return next(err);
    var layout = layouts[0];

    console.log(layout.blocks);

    layout.blocks.set(0, {full: false, fields: openFields});
    layout.markModified('blocks');

    console.log(layout.blocks);

    layout.save(function(err){
      console.log('saved: ', err);
      next(err);
    });
  });
});

console.log 值是

[{ full: true, _id: 54147307f07097462fb93912, fields: [] }]
[{ full: false,
  _id: 54147307f07097462fb93918,
  fields: [ 'one', 'two' ] }]
saved:  null

然后我检查保存的布局并得到:

blocks: [ { full: false, _id: 54147307f07097462fb93918, fields: [] } ],

所以,_idfull 已保存,但 fields 未保存!

如果我直接更新会发生类似的事情

Layouts.update({_company: that._company, object: that._id, default: true},
    {$set: {'blocks.0.fields': openFields}},
    function(err){
      next(err);
  });

有什么建议吗?

【问题讨论】:

    标签: arrays string node.js mongodb mongoose


    【解决方案1】:

    我尝试实现您指定的相同架构,并尝试以三种不同的方式更新我的布局对象,所有这些都有效。这是我的实现和测试。从您提交的代码中看不到您的错误所在。

    var mongoose = require('mongoose');                                              
    mongoose.connect('mongodb://localhost/layout-test');                                    
    
    var LayoutSchema = new mongoose.Schema({                                         
        blocks: [{                                                                   
            full: {type: Boolean},                                                   
            fields: [{                                                               
                type: String                                                         
            }],                                                                      
        }],                                                                          
    });                                                                              
    var Layout = mongoose.model('Layout', LayoutSchema);                             
    var LAYOUT_DATA = {                                                                 
        blocks: [                                                                
            {full: true},                                                        
        ],                                                                       
    };
    
    // Updating the object, setting the first item of blocks
    var test1 = function() {                                                        
        new Layout(LAYOUT_DATA).save(function(err, layout) {                                                             
            Layout.update({                                                          
                _id: layout._id                                                      
            }, {                                                                     
                $set: { 
                    'blocks.0.full': false,                                                        
                    'blocks.0.fields': ['one', 'two'],                               
                },                                                                   
            }, function(err, nbrOfUpdates) {                                                
                Layout.findById(layout._id, function(err, layout) {        
                    if (layout.blocks[0].fields.length === 0) {
                        throw Error();
                    }                                                                
                });                                                                  
            });                                                                      
        });                                                                          
    };                                                                               
    
    // Replacing the old blocks with a new array                                                                      
    var test2 = function() {                                                       
        new Layout(LAYOUT_DATA).save(function(err, layout) {                                                 
            layout.blocks = [{                                                       
                full: false,                                                         
                fields: ['one', 'two'],                                              
            }];                                                              
            layout.save(function(err, layout) {  
                if (layout.blocks[0].fields.length === 0) {
                    throw Error();
                }                                                                    
            });                                                                      
        });                                                                          
    };         
    
    // Pushing a new object to the blocks array and splicing away the old one
    var test3 = function() {                                                      
        new Layout(layout).save(function(err, layout) {                                                 
            layout.blocks.push({                                                     
                full: false,                                                         
                fields: ['one', 'two'],                                              
            });                                                                      
            layout.blocks.splice(0, 1);                                              
            layout.save(function(err, layout) {           
                if (layout.blocks[0].fields.length === 0) {
                    throw Error();
                }                                                 
            });                                                                      
        });                                                                          
    };
    

    【讨论】:

    • 是的,我只是想不出任何可能使操作失败的事情......即使有一些东西可以触发 __v 错误,例如(并且什么都没有)它会在某处引发错误。无论如何,感谢您的尝试。
    猜你喜欢
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2013-12-08
    • 1970-01-01
    • 2020-12-04
    • 2018-07-12
    • 2021-01-30
    相关资源
    最近更新 更多