node.js 如何使用猫鼬 findOne
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7033331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use mongoose findOne
提问by Callum Rogers
I have the below schema (apologies that it is in coffeescript)
我有以下模式(抱歉,它是在 coffeescript 中)
Schema = mongoose.Schema
AuthS = new Schema
auth: {type: String, unique: true}
nick: String
time: Date
Auth = mongoose.model 'Auth', AuthS
I simply want to recover one record which is definitely in my database:
我只想恢复一条肯定在我的数据库中的记录:
Auth.findOne({nick: 'noname'}, function(obj) { console.log(obj); });
Unfortunately this always logs null. db.auths.findOne({nick: 'noname'})in mongo shell always returns a value. What is going on?
不幸的是,这总是记录null。db.auths.findOne({nick: 'noname'})在 mongo shell 中总是返回一个值。到底是怎么回事?
回答by Callum Rogers
Found the problem, need to use function(err,obj)instead:
发现问题,需要function(err,obj)改用:
Auth.findOne({nick: 'noname'}, function(err,obj) { console.log(obj); });
回答by Isaac Pak
Mongoose basically wraps mongodb's api to give you a pseudo relational db api so queries are not going to be exactly like mongodb queries. Mongoose findOnequery returns a query object, not a document. You can either use a callback as the solution suggests or as of v4+ findOnereturns a thenable so you can use .then or await/async to retrieve the document.
Mongoose 基本上包装了 mongodb 的 api,为您提供一个伪关系数据库 api,因此查询不会与 mongodb 查询完全一样。Mongoose findOne查询返回一个查询对象,而不是一个文档。您可以按照解决方案的建议使用回调,或者从 v4+ findOne 开始返回 thenable,以便您可以使用 .then 或 await/async 来检索文档。
// thenables
Auth.findOne({nick: 'noname'}).then(err, result) {console.log(result)};
Auth.findOne({nick: 'noname'}).then(function (doc) {console.log(doc)});
// To use a full fledge promise you will need to use .exec()
var auth = Auth.findOne({nick: 'noname'}).exec();
auth.then(function (doc) {console.log(doc)});
// async/await
async function auth() {
const doc = await Auth.findOne({nick: 'noname'}).exec();
return doc;
}
auth();
See the docsif you would like to use a third party promise library.
如果您想使用第三方承诺库,请参阅文档。
回答by Bhagvat Lande
In my case same error is there , I am using Asyanc / Await functions , for this needs to add AWAIT for findOne
在我的情况下,存在同样的错误,我使用的是 Asyanc/Await 函数,为此需要为 findOne 添加 AWAIT
Ex:const foundUser = User.findOne ({ "email" : req.body.email });
above , foundUser always contains Object value in both cases either user found or not because it's returning values before finishing findOne .
上面,在两种情况下,foundUser 始终包含 Object 值,无论用户是否找到,因为它在完成 findOne 之前返回值。
const foundUser = await User.findOne ({ "email" : req.body.email });
above , foundUser returns null if user is not there in collection with provided condition . If user found returns user document.
上面,如果用户不在集合中并提供条件,则 foundUser 返回 null。如果用户找到,则返回用户文档。
回答by Neil Guy Lindberg
You might want to consider using console.logwith the built-in "arguments" object:
您可能需要考虑使用console.log内置的“参数”对象:
console.log(arguments); // would have shown you [0] null, [1] yourResult
This will always output all of your arguments, no matter how many arguments you have.
无论您有多少个参数,这将始终输出您的所有参数。
回答by NaturalCoder
Use obj[0].nick and you will get desired result,
使用 obj[0].nick 你会得到想要的结果,

