我在mongodb中使用nodejs收集了这个集合:
_id:
name:
email:
date:
我想使用最大_id,我想要整数值。我尝试此代码,但发现最大后我不知道如何使用。
db.collection.find().sort({_id:-1}).limit(1)
我尝试使用find和findone编写很多代码,但是我无法获得_id中的最大整数。我不使用十六进制ID。我需要使用此ID创建未使用的ID的新帐户,而最后一个帐户是最大_id。有人有主意吗?
以下代码以最大值打印文档_id
(在mongo shell中尝试过):
var myCursor = db.test.find().sort( { _id: -1 } ).limit(1);
if (myCursor.hasNext()) {
let doc = myCursor.next();
printjson(doc);
}
所述db.collection.find()方法返回一个光标。该光标有很多,你可以应用它,做各种功能的方法:光标的方法。
db.test.find()
.sort( [[ '_id', -1 ]] )
.limit(1)
.toArray( (err, docs) => {
if (docs.length > 0) {
let maxId = docs[0]._id;
console.log(maxId);
}
else {
console.log('Nothing found!');
} );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句