我是 Mongodb 的新手。我收到以下错误 -
E QUERY [thread1] TypeError: db.events.findOne(...).sort is not a function :
db.events.findOne({
"userId" : {
"$ne" : ""
},
"$and" : [
{
"userId" : {
"$exists" : true
}
}
]
}).sort({
"_id" : -1
});
Run Code Online (Sandbox Code Playgroud)
当我添加限制时,它可以工作,但如果没有限制,它就不起作用。
非常感谢任何帮助。提前致谢。
sort()不能用findOne()这种方式使用。我建议使用以下内容:
db.events.find({
"userId" : {
"$ne" : ""
},
"$and" : [
{
"userId" : {
"$exists" : true
}
}
]
}).sort({
"_id" : -1
}).limit(1)
Run Code Online (Sandbox Code Playgroud)