use*_*205 3 mongodb node.js 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' }
}
arr = [{
'id':attResult.id,
'subtype':type,
'title' : attResult.title,
'body' : attResult.body,
'filetype' : attResult.filetype
}];
Run Code Online (Sandbox Code Playgroud)
我正在尝试将附件推送到文档唯一的“附件”数组中。
这是我的查询。
books.update(
{ id: refid },
{ $push: { attachments: arr } }
).done(function (err, updElem) {
console.log("updElem" + JSON.stringify(updElem));
});
Run Code Online (Sandbox Code Playgroud)
我的查询有什么问题,没有错误但没有更新附件。
我希望我的结果是这样的:
{
"_id" : 5,
"attachments": [
{
"id": "xxxxxxx",
"subtype": "book",
"title": "xxxx",
"body": "xxxx" ,
"filetype" : "xxxxx"
},
{
"id": "xxxxxxx",
"subtype": "book",
"title": "xxxx",
"body": "xxxx",
"filetype": "xxxxx"
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在可以使用本机 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'
}
];
Run Code Online (Sandbox Code Playgroud)
以下查询可用于将数组元素推送到最后的“附件”。$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));
});
Run Code Online (Sandbox Code Playgroud)
这会将数组中的每个对象插入到附件中
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));
});
Run Code Online (Sandbox Code Playgroud)
再看看你的问题,我敢打赌你实际上在这里使用了“帆”,即使你的问题没有被标记为这样。
这里的问题是,水线 ODM/ORM 对于实际支持什么类型的操作有自己的想法,因为它试图在使用 SQL/NoSQL 后端和某种要求某种可能的操作之间保持不可知。
结果是,$push目前并没有真正支持更新,您需要更多的 JavaScript 操作。所以实际上你需要通过.findOneand.save()操作来操纵它:
books.findOne(refid).exec(function(err,book) {
book.attachments.push( arr[0] );
book.save(function(err){
// something here
});
});
Run Code Online (Sandbox Code Playgroud)
其中一部分是“水线”简写,否则将被视为_id和id作为术语的可互换使用,其中仅将id值指定为单个参数意味着您正在引用id查询选择中的值。
因此,除非您更换水线 ODM/ORM,否则您几乎会被这个 AFAIK 困住,直到决定以与 MongoDB API 更一致的方式维护此逻辑,或者允许访问“原始”驱动程序接口来执行这些.update()操作。
不过,作为参考,并且已经提到过,您的通用“shell”语法或 MongoDB 特定驱动程序中支持的语法是这样的,弃用了运算符,其目的是使用修饰符将$pushAll功能与$pushand运算符合并:$addToSet$each
db.collection.update(
{ "_id": ObjectId(refid) }, // should be important to "cast"
{
"$push": {
"attachments": {
"$each": arr
}
}
}
)
Run Code Online (Sandbox Code Playgroud)
因此,该语法在适用的地方可以工作,但对你来说,我认为在“帆”中它不会。
这会给你一些思考的空间,并让你深入了解做事的正确方法。
| 归档时间: |
|
| 查看次数: |
24461 次 |
| 最近记录: |