从我们刚刚保存的实体获取ID的最佳方法? [英] Best way to get the id from an entity we just saved?
本文介绍了从我们刚刚保存的实体获取ID的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种方法可以获取刚用猫鼬保存的实体的ID(在BDD中自动生成).
I would like to know if there is a way to get the ID (automatically generated in the BDD) of an entity we just saved with mongoose.
也许在保存方法上有一个回调或其他东西,除了错误回调外,我在文档中什么都没找到.
Maybe there is a callback or something on the save method, i didn't find anything about it on the documentation, except the error callback.
谢谢.
推荐答案
新创建的文档的_id
是在客户端而不是服务器上创建的.因此它实际上在您调用save
之前就可用.
The _id
of a newly created document is created on the client, not the server. So it's actually available before you even call save
.
var newDoc = new MyModel({name: 'foobar'});
console.log(newDoc._id); // logs the assigned _id
newDoc.save(function(err, doc) {
console.log(doc._id); // also available here; newDoc._id == doc._id
});
这篇关于从我们刚刚保存的实体获取ID的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文