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

User.find({},function(err,docs){ foreach(docs as d){d.name="apple";d.save();};}); This doesn&

User.find({},function(err,docs){ 
            foreach(docs as d){
                d.name="apple";
                d.save();
            };
        }); 

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

User.find({},function(err,docs){ 
            foreach(docs as d){
                d.name="apple";
                d.save();
            };
        }); 

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

Share Improve this question edited Sep 22, 2017 at 17:57 CommunityBot 11 silver badge asked Apr 27, 2011 at 18:06 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 1
  • not knowing anything about mongoose and only just today saw a demo of Node, perhaps giving all your docs the name "apple" is not right? Can you do d.name="apple"+(i++); and define var i=0 before the foreach? Or are you talking about search.cpan/dist/Mongoose/lib/Mongoose/Intro.pod#_id – mplungjan Commented Apr 27, 2011 at 18:21
Add a ment  | 

2 Answers 2

Reset to default 6

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744724284a4590089.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信