javascript 节点 js/猫鼬 .find()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48255659/
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
Node js / Mongoose .find()
提问by Echo
Im having trouble using the .find() function within mongoose on a node js server I've been trying to use this but I cannot get the key information out of my database.
我在 node js 服务器上的 mongoose 中使用 .find() 函数时遇到问题我一直在尝试使用它,但我无法从我的数据库中获取关键信息。
user.find({key: 1} , function(err, data){
if(err){
console.log(err);
};
console.log("should be the key VVV");
console.log(data.key);
});
I'm mainly just having trouble wrapping my head around how this function takes queries and gives you back the response from your DB. If someone can break it down id be very thankful the mongoose docs weren't much help.
我主要只是在思考这个函数如何进行查询并从数据库中返回响应时遇到问题。如果有人可以分解它,我将非常感谢猫鼬文档并没有太大帮助。
Also this is my user schema if it helps
如果有帮助,这也是我的用户架构
var userSchema = new mongoose.Schema({
username: {type: String, unique: true},
password: {type: String},
key: {type: String},
keySecret: {type: String}
}, {collection: 'user'});
var User = mongoose.model('user',userSchema);
module.exports = User;
回答by David Alsh
If you imagine your DB looking like this:
如果你想象你的数据库是这样的:
[
{
"name": "Jess",
"location": "Auckland"
},
{
"name": "Dave",
"location": "Sydney"
},
{
"name": "Pete",
"location": "Brisbane"
},
{
"name": "Justin",
"location": "Auckland"
},
]
executing the following query;
执行以下查询;
myDB.find({location: 'Brisbane'})
myDB.find({location: 'Brisbane'})
will return:
将返回:
[
{
"name": "Pete",
"location": "Brisbane"
}
]
While myDB.find({location: 'Auckland'})will give you
虽然myDB.find({location: 'Auckland'})会给你
[
{
"name": "Jess",
"location": "Auckland"
},
{
"name": "Justin",
"location": "Auckland"
},
]
As you can see, you're looking through the array for a key that matches the one you're looking to findand gives you back all of the documents that match that key search in the form of an array.
如您所见,您正在通过数组查找与您要查找的键匹配的键,find并以数组的形式返回与该键搜索匹配的所有文档。
The Mongoose interface gives this data to you in the form of a callback, and you just need to look for the item inside of the array it returns
Mongoose 接口将这些数据以回调的形式提供给你,你只需要在它返回的数组中查找项目即可
user.find({location: "Auckland"}, function(err, data){
if(err){
console.log(err);
return
}
if(data.length == 0) {
console.log("No record found")
return
}
console.log(data[0].name);
})
回答by lutaoact
Maybe you should use
也许你应该使用
Model.findOne({key: '1'}, function(err, data) {
console.log(data.key);
});
find()will get a doc array, and findOne()can get just one doc.
find()将获得一个 doc 数组,并且findOne()只能获得一个 doc。
Your field keyis Stringtype, so your query obj shoule be {key: '1'}, not {key: 1}.
您的字段key是String类型,因此您的查询 obj 应该是{key: '1'},而不是{key: 1}。
Read the mongoose docs carefully may help you.
仔细阅读猫鼬文档可能会对您有所帮助。
回答by bahri noredine
hi my friend i use this methodto retrieve data from db i hope that can help
嗨,我的朋友,我使用这种方法从数据库中检索数据,希望对您有所帮助
user.find({key: 1})
.then((userOne)=>{
//if is an API
res.status(200).json({data : userOne});
//OR
// if you had a view named profile.ejs for example
res.render('/profile',{data : userOne, title : 'User profile' });
console.log(userOne); // just for check in console
})
.catch((error)=>{
//if an api
res.status(400).json({messageError : error});
// OR
if not an api
res.render('/profile',{data : 'No data of this profile',errorToDisplay : error})
});
});

