如果存在则如何更新,否则在 javascript/node.js 中插入新文档? 我将作为函数字典的参数,如果字典包含 _id 应该更新,否则插入远程服务器(我通过 mongoose 与远程服务器连接,并且我有要插入/更新的 Person 模式)。
最佳答案
在 Mongoose 中,您将使用 Person.update
per the documentation .为了创建一个不存在的文档,您需要在选项哈希中传递 { upsert : true }
,因为它默认为 false
。
即
Person.update( { name : 'Ted' }, { name : 'Ted', age : 50 }, { upsert : true }, callback );
https://stackoverflow.com/questions/21342747/