Mongoose多更新 [英] Mongoose multi update

查看:141
本文介绍了Mongoose多更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的数据库看起来像这样。

  [
{
_id:1,
value:50
},
{
_id:2,
value:100
}
]

此查询返回错误,因为我传递一个数组而不是$集合中的对象。

 模型。更新({_ id:{$ in:ids}},{$ set:ids.value},{multi:true}; 

我希望我的数据库看起来像这样

  [
{
_id:1,
value:4
},
{
_id:2,
value:27
}
]


解决方案

首先你的 update()查询不正确。



查看此文档 here


Model.update(条件,更新,选项,回调);


db模型包含这样的文档(如您所述的文档):

  [
{
_id:1,
value:50
},
{
_id:2,
value:100
}
];

并且您的下面的数组包含要使用当前数据库文档修改的对象(即,docs)喜欢这样:

  idsArray:[
{
_id:1,
value:4
},
{
_id:2,
value:27
}
];

根据我对mongodb& mongoose,我不认为你可以用单行查询更新所有的文档(这是你想要做的)..(PS我不知道,所以我不知道这个..)



但是为了使你的代码工作,你会做这样的事情:



想法:循环文档中的每个文档ie , idsArray 并调用update()。



所以,这里是代码:

  idsArray.forEach(function(obj){
Model.update({_ id:obj._id},{$ set:{ value:obj.value}});
});

在上面的代码中,我假设你有 _id 在db docs中的值,就像上面写的那样(即_ id:1 )..但是如果他们像这样 _id:ObjectId('1')

  [
{
_id:ObjectId('1'),
value:50
},
.....
.....
]

那么你需要转换 _id to ObjectId(obj._id) in update()query ..
所以你会这样做。

  var ObjectId = require('mongodb')。ObjectID; 

idsArray.forEach(function(obj){
Model.update({_ id:ObjectId(obj._id)},{$ set:{value:obj.value}});
});

只需确认(即 _id ),然后再转发。



希望这有帮助。



干杯,


I want to update multiple docs with different values.

My Database looks something like this.

[
    {
        "_id": 1,
        "value": 50
    },
    {
        "_id": 2,
        "value": 100
    }
]

This Query return an error because i'm passing an array instead of an object in the $set.

    Model.update({_id: {$in: ids}}, {$set: ids.value}, {multi: true};

I want my database to look like this

[
        {
            "_id": 1,
            "value": 4
        },
        {
            "_id": 2,
            "value": 27
        }
    ]

解决方案

First of all your update() query is not ok..

See documentation for this here

Model.update(conditions, update, options, callback);

Suppose your current db model contains docs like this (as you described in question as well):

[
    {
        "_id": 1,
        "value": 50
    },
    {
        "_id": 2,
        "value": 100
   }
];

and you've below array which contains objects (i.e., docs) to be modified with current db's docs to like this:

idsArray: [
    {
        "_id": 1,
        "value": 4
    },
    {
        "_id": 2,
        "value": 27
    }
];

From my experience with mongodb & mongoose, i don't think you can update all docs with single line query (that's you're trying to do).. (P.S. I don't know about that so I am not sure to this..)

But to make your code work, you will be doing something like this:

Idea: Loop over each doc in docs i.e, idsArray and call update() over it..

So, Here's code to this:

idsArray.forEach(function(obj) {
    Model.update({_id: obj._id}, {$set: {value: obj.value}});
});

In above code, I am supposing you've _id values in db docs as they 're written above (i.e, "_id": 1).. But if they're like this "_id": ObjectId('1')

[
  {
    "_id": ObjectId('1'),
    "value": 50
  },
  .....
  .....
]

then you'll be need to convert _id to ObjectId(obj._id) in update() query.. so for that you'll be doing like this.

var ObjectId = require('mongodb').ObjectID;

idsArray.forEach(function(obj) {
   Model.update({_id: ObjectId(obj._id)}, {$set: {value: obj.value}});
});

P.S. Just confirm it (i.e., _id) before go forward..

Hope this helps.

Cheers,

这篇关于Mongoose多更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆