admin管理员组文章数量:1391943
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 definevar 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
2 Answers
Reset to default 6I 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.
本文标签: javascriptIn Nodejs MongooseI want to update each fieldhow can I do thatStack Overflow
版权声明:本文标题:javascript - In Node.js Mongoose, I want to update each field...how can I do that? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744724283a2621875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论