[英]mongoose upsert silently failling
I have a question about the way I am using the "upsert" query on mongoose. 我对猫鼬使用“ upsert”查询的方式有疑问。 For some reason for a particular model it does not insert a new model even when upsert is true.
由于某种原因,即使upsert为true,它也不会插入新模型。 It also doesn't send out an error telling me why it didn't update.
它也不会发出错误告诉我为什么它没有更新。 It just returns numAffected=0, here is my current method
它只是返回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!");
}
});;
Here is a copy of the model for reference 这是模型的副本以供参考
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);
Note that this works properly when updating just not when inserting. 请注意,这在更新时正常工作,而在插入时不行。 Also im sorry if this is too wordy and please tell me if I should cut out my schema from the question.
如果这太罗word,也很抱歉,请告诉我是否应该从问题中删除我的架构。
Kevin B mentioned in the comments to turn on debugging. Kevin B在注释中提到要打开调试。 I did and found the following:
我找到了以下内容:
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' } } {} –
The options were not being read properly!! 选项未正确阅读!
The correct format for options is 选项的正确格式是
options = {upsert:true, runValidators:true}
not 不
options = [{upsert:true},{runValidators:true}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.