我有以下代码:
getLastInsertId = function(collection, field){
field = (typeof field === "undefined") ? '_id' : field;
var qry = JSON.parse('{"fields" :{"'+field+'": 1}, "sort" : {"$natural": -1}, "limit": 1}');
//console.log(qry);
var lastInsertId = collection.find({}, qry).fetch()[0];
if(lastInsertId === 'undefined'){
//var x = 1;
return "1";
//console.log(Number(1));
}else{
//return (+lastInsertId + 1);
}
};
如果我使用以下内容,则返回NaN:
return 1;
有人可以指导我,我做错了什么以及如何纠正。
答案 0 :(得分:1)
试试这个
var qry = {"fields" :{"'+field+'": 1}, "sort" : {"$natural": -1}, "limit": 1};
//console.log(qry);
var lastInsertId = collection.findOne(qry);
lastInsertId = lastInsertId._id;