有没有办法在upsert后得到记录_id?
我已经看过这篇文章(如何使用mongoose将文档插入mongodb并获取生成的id?),但这只是针对插入而不是更新.
此外,使用MongoDB,您可以使用getlasterror(https://groups.google.com/forum/?fromgroups=#!topic/mongoose-orm/ehZ11QY-OUw)获取_id ,但Mongoose不提供访问权限.它(https://groups.google.com/forum/?fromgroups=#!topic/mongoose-orm/pSv6WrasvWg)
谢谢
Tom*_*rth 10
在options对象中使用Mongoose的findOneAndUpdate方法upsert: true.
var query = { name: 'borne' },
data = { name: 'jason borne' },
options = { upsert: true };
Model.findOneAndUpdate(query, data, options, function (err, object) {
/* use object._id */
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4427 次 |
| 最近记录: |