In Node.js Mongoose, I want to update each field…how can I do that?
-
24-10-2019 - |
Pergunta
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?
Solução
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.
Outras dicas
Does d
have a unique index set? If so you will be unable to set the same thing for multiple instances.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow