This content originally appeared on CodeSource.io and was authored by Deven
In this article, you are going to learn about how to query in Mongoose.
Mongoose is an ODM(Object Data Modeling) library for MongoDB that is based on NodeJS. You can create Schema, manage documents, and many more by using Mongoose. Moreover, you can query specific data. Here, query refers to the term as finding data from MongoDB database. To find data by using mongoose we need to use the mongoose find() function.
Before using the find() function for querying a specific data from MongoDB, let’s see which data we will query in the below:
Here, in our MongoDB database, we have five documents of students with their names and age.
Let’s see step by step how we can query-specific data from the MongoDB database by using the mongoose find() function.
Model
const mongoose = require('mongoose')
const studentSchema = new mongoose.Schema({
name : {
type : String
},
age : {
type : String
},
})
const Student = mongoose.model('Student',studentSchema)
module.exports = Student
This is our Student database model where we are saving only two documents’ names and ages and you can see the documents are being saved in the database. Let’s query specific data based on names in the below:
JavaScript
const mongoose = require('mongoose')
const express = require('express')
const dotenv = require('dotenv')
const Student = require('./Student')
dotenv.config({path:'./config.env'})
const app = express()
// Connecting Database
const DB = process.env.DATABASE.replace('<PASSWORD>',process.env.DATABASE_PASSWORD)
mongoose.connect(DB,{
useNewUrlParser : true,
useUnifiedTopology : true
}).then(() => {
console.log('Database Connected...')
})
// Start servr on a specific port
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`App is running on port ${PORT}...`);
});
// Query a data on the basis of name
Student.find({ name: 'deven'},(err, data) => {
if (err){
console.log(err);
}
else{
console.log(data);
}
});
Here, we connect our database and keep this information in the config file. I assume that you already know about basic creating a server and connecting a database and other stuff. Finally, we query data from the MongoDB document using the find function. Let’s see the output in the console:
You can see that we successfully query a document from our database. Let’s query data based on age and we know that there are two documents that exist with the same age and that is 22. Let’s see if it returns two documents or not:
// Query a data on the basis of name
Student.find({ age: '22'},(err, data) => {
if (err){
console.log(err);
}
else{
console.log(data);
}
});
Output
You can see that we are getting exactly the data that we have expected. This is a demo and this find() function is quite useful for querying specific data from millions of data.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2022-01-11T17:24:13+00:00) How to query in Mongoose. Retrieved from https://www.scien.cx/2022/01/11/how-to-query-in-mongoose/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.