Node.js mongodb更新推送阵列
我有下面的模式。我在mongodb中使用node.jsNode.js mongodb更新推送阵列,node.js,mongodb,sails.js,Node.js,Mongodb,Sails.js,我有下面的模式。我在mongodb中使用node.js attributes: { type: { type: 'string' }, title: { type:'string' }, description: { type:'string' }, is_active: { type:'boolean',defaultsTo:true }, createdBy: { type:'json' }, attachments:{ type:'array
attributes: {
type: { type: 'string' },
title: { type:'string' },
description: { type:'string' },
is_active: { type:'boolean',defaultsTo:true },
createdBy: { type:'json' },
attachments:{ type:'array' }
}
arr = [{
'id':attResult.id,
'subtype':type,
'title' : attResult.title,
'body' : attResult.body,
'filetype' : attResult.filetype
}];
我正在尝试将附件推送到文档唯一的“附件”数组中
这就是我的问题所在
books.update(
{ id: refid },
{ $push: { attachments: arr } }
).done(function (err, updElem) {
console.log("updElem" + JSON.stringify(updElem));
});
我的查询有什么问题,没有错误,但没有更新的附件
我希望我的结果是:
{
"_id" : 5,
"attachments": [
{
"id": "xxxxxxx",
"subtype": "book",
"title": "xxxx",
"body": "xxxx" ,
"filetype" : "xxxxx"
},
{
"id": "xxxxxxx",
"subtype": "book",
"title": "xxxx",
"body": "xxxx",
"filetype": "xxxxx"
}
]
}
您正在尝试将
数组作为元素插入数组中。您可能希望将$pushAll
作为短期解决方案。但是,不推荐使用此运算符
或者,您可以简单地迭代您的数组,每次迭代都将数组中的一个元素推送到附件中(这是Mongo devs推荐的方法)。再看一看您的问题,我敢打赌,即使您的问题没有被标记为“sails”,您实际上在这里使用了“sails”
这里的问题是,waterline ODM/ORM对实际支持哪种操作有自己的想法,因为它试图在使用SQL/NoSQL后端和某种可能的操作需求之间保持不可知
其结果是,目前并不真正支持使用的更新,您需要更多的JavaScript操作。因此,事实上,您需要通过.findOne
和.save()
操作来处理此操作:
books.findOne(refid).exec(function(err,book) {
book.attachments.push( arr[0] );
book.save(function(err){
// something here
});
});
其中一部分是“水线”的缩写,它被认为是\u id
和id
作为术语的可互换用法,其中仅将id
值指定为单个参数就意味着您在查询选择中引用了id
值
因此,除非您更换waterline ODM/ORM,否则在决定以更符合MongoDB API的方式维护此逻辑或允许访问“原始”驱动程序接口以执行这些.update()
操作之前,您几乎无法使用此AFAIK
不过,作为参考,您的通用“shell”语法或MongoDB特定驱动程序中支持的其他语法如下所示,不推荐使用运算符,目的是使用修饰符将功能与and运算符合并:
db.collection.update(
{ "_id": ObjectId(refid) }, // should be important to "cast"
{
"$push": {
"attachments": {
"$each": arr
}
}
}
)
所以,这种语法在它适用的地方会起作用,但对你们来说,我认为在《风帆》中它不会起作用
这给了您一些思考的食粮,也让您对正确的操作方式有了一些见解。现在,可以使用本机mongodb库将元素推入数组
考虑以下mongodb集合对象
{
"_id" : 5,
"attachments": [
{
"id": "xxxxxxx",
"subtype": "book",
"title": "xxxx",
"body": "xxxx" ,
"filetype" : "xxxxx"
},
{
"id": "xxxxxxx",
"subtype": "book",
"title": "xxxx",
"body": "xxxx",
"filetype": "xxxxx"
}
]
}
arr = [{
'id':'123456',
'subtype':'book',
'title' : 'c programing',
'body' :' complete tutorial for c',
'filetype' : '.pdf'
},
{
'id':'123457',
'subtype':'book',
'title' : 'Java programing',
'body' :' complete tutorial for Java',
'filetype' : '.pdf'
}
];
以下查询可用于将数组元素推送到末尾的“附件”$push或$addToSet可用于此操作
这将在附件中插入一个对象或元素
db.collection('books').updateOne(
{ "_id": refid }, // query matching , refId should be "ObjectId" type
{ $push: { "attachments": arr[0] } } //single object will be pushed to attachemnts
).done(function (err, updElem) {
console.log("updElem" + JSON.stringify(updElem));
});
db.collection('books').updateOne(
{ "_id": refid }, // query matching , refId should be "ObjectId" type
{ $push: { "attachments":{$each: arr} } } // arr will be array of objects
).done(function (err, updElem) {
console.log("updElem" + JSON.stringify(updElem));
});
这将把数组中的每个对象插入到附件中
db.collection('books').updateOne(
{ "_id": refid }, // query matching , refId should be "ObjectId" type
{ $push: { "attachments": arr[0] } } //single object will be pushed to attachemnts
).done(function (err, updElem) {
console.log("updElem" + JSON.stringify(updElem));
});
db.collection('books').updateOne(
{ "_id": refid }, // query matching , refId should be "ObjectId" type
{ $push: { "attachments":{$each: arr} } } // arr will be array of objects
).done(function (err, updElem) {
console.log("updElem" + JSON.stringify(updElem));
});
嗨,马丁,我试过$pullAll,但还是一样的问题。请帮助我如何解决此问题。您在这里真正使用的是什么?也许是猫鼬?你是说“属性”是你的模式定义吗?或者该定义实际上是一个名为“attributes”的字段下的附加字段吗?谢谢大家。我尝试了“\u id”方法。这也是一个同样的问题。但我尝试了mongodb保存方法。谢谢大家。@user3046205所以你在使用sails?我解释了为什么这是使用sails的唯一方法,即检索数据,然后进行操作,然后使用.save()
。如果你确认了这一点,我们就可以对你的问题进行重新分类并接受答案,让人们知道有一个解决方案。添加一些解释来支持你的答案阿育王