猫鼬 upsert 默默失败

mongoose upsert silently failling

我对在 mongoose 上使用 "upsert" 查询的方式有疑问。由于某种原因,对于特定模型,即使 upsert 为真,它也不会插入新模型。它也不会发出错误告诉我为什么它没有更新。它只是 returns 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); 

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

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}]