繁体   English   中英

猫鼬upsert默默失败

[英]mongoose upsert silently failling

我对猫鼬使用“ upsert”查询的方式有疑问。 由于某种原因,即使upsert为true,它也不会插入新模型。 它也不会发出错误告诉我为什么它没有更新。 它只是返回numAffected = 0,这是我当前的方法

query = {gatewayId:req.body.gatewayId};
body  = {$push:{xbees:{$each: req.body.xbees}},
          "$setOnInsert":{address:req.body.address}};
options = [{upsert:true},{runValidators:true}]
GatewayData.update(query,body,options,function(err,numAffected,rawResposne){
    if (err) return next(err);
        if(numAffected == 0){
          console.log("ohno!");
    }
});;

这是模型的副本以供参考

    var xbeeMapping = new mongoose.Schema({
       ...
    });

    var GatewayData = new mongoose.Schema({

        address:       {type: String, uppercase:true, required:true},
        gatewayId:     {type: String, match : validators.gateway_matcher, required: true},
        timestamp:     {type: Date,   default: Date.now},
        panID:         {type: String, uppercase:true},
        radThreshold:  {type: Number, default:30},
        roomThreshold: {type: Number, default:22.22},
        radCritical:   {type: Number, default:15},
        roomCritical:  {type: Number, default:19.5},

        xbees:[xbeeMapping] 

    });

    GatewayData.index({gatewayId:1});

module.exports = mongoose.model('GatewayData', GatewayData); 

请注意,这在更新时正常工作,而在插入时不行。 如果这太罗word,也很抱歉,请告诉我是否应该从问题中删除我的架构。

Kevin B在注释中提到要打开调试。 我找到了以下内容:

Mongoose: gatewaydatas.update({
     gatewayId: '00000000-00000000-00409DFF-FFFFFFFD' }) 
     { '$push':
         { xbees: 
               { '$each': 
           [ { serialNumber: '40AC1233', 
               roomNumber: 'LR', xbeeAddress: 
                 'NODE_[40:AC:12:33]!', 
                _id: ObjectId("55563d50f9c72ca75c15612c") } ] } },         
     '$setOnInsert': { address: 'Stephens' } } {} – 

选项未正确阅读!

选项的正确格式是

  options = {upsert:true, runValidators:true} 

  options = [{upsert:true},{runValidators:true}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM