我正在使用loopback + mongoDB开发API。因为我必须更新 mongoDB 文档中的数组:
我的模型:
{
"userId": "546fgdfgd445",
"rrrr": "7",
"bbbbb": [
{}
],
"id": "57a437789521b58b12124016"
}
我需要使用从客户端发送的数据来使用新元素更新数组:
app.models.vvvvv.update({ userId: '546fgdfgd445' },{ $push: { "transactions": transactionData }}, function(err, res){
if(err){
console.log(err);
} else {
console.log(res);
}
});
但我收到错误:
{ name: 'MongoError',
message: 'The dollar ($) prefixed field \'$push\' in \'$push\' is not valid for storage.',
driver: true,
index: 0,
code: 52,
errmsg: 'The dollar ($) prefixed field \'$push\' in \'$push\' is not valid for storage.' }
我使用的是 mongoDb 版本 3.2.3。
请分享您的想法。提前致谢。
最佳答案
在 model.json
中,您需要启用如下扩展操作:
"options": {
"mongodb": {
"collection": "model_collection",
"allowExtendedOperators": true
}
},
或
app.models.vvvvv.update({ userId: '546fgdfgd445' },
{ $push: { "transactions": transactionData }}, { allowExtendedOperators: true }, function(err, res){
if(err){
console.log(err);
} else {
console.log(res);
}
});
关于node.js - 通过我的环回应用程序更新 mongoDB 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38783053/