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.
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;
The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).
mongoose .find() method returns object with unwanted properties. 12. Model.findOne not returning docs but returning a wrapper object.
Mongoose queries are not promises. They have a . then() function for co and async/await as a convenience.
As the name implies, findOneAndUpdate() finds the first document that matches a given filter , applies an update , and returns the document. By default, findOneAndUpdate() returns the document as it was before update was applied. You should set the new option to true to return the document after update was applied.
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'})
will return:
[
{
"name": "Pete",
"location": "Brisbane"
}
]
While myDB.find({location: 'Auckland'})
will give you
[
{
"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 find
and gives you back all of the documents that match that key search in the form of an array.
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
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);
})
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.
Your field key
is String
type, so your query obj shoule be {key: '1'}
, not {key: 1}
.
Read the mongoose docs carefully may help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With