开发者

In Node.js Mongoose, I want to update each field...how can I do that?

开发者 https://www.devze.com 2023-02-28 22:29 出处:网络
User.find({},function(err,docs){ foreach(docs as d){ d.name=\"apple\"; d.sav开发者_运维问答e(); };
User.find({},function(err,docs){ 
            foreach(docs as d){
                d.name="apple";
                d.sav开发者_运维问答e();
            };
        }); 

This doesn't work! I get some "unique identifier" error. Can someone fix this for me?


I think you're using foreach incorrectly. Try replacing the contents of your callback with this:

docs.forEach(function(elem, index, array) {
    elem.name = "apple";
    elem.save();
});

Check out the MDC for more information on foreach.


Does d have a unique index set? If so you will be unable to set the same thing for multiple instances.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号