Fetching Data from DB

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 278:- Fetching Data from DB

To fetch data from MongoDB using Mongoose, we can make use of the find() method of the Model class. The find() method allows us to query the database and retrieve documents that match the specified criteria.

Here's an example code snippet that shows how to fetch all documents from a collection:

javascriptCopy code

const mongoose = require('mongoose'); const { Contact } = require('./models'); mongoose.connect('mongodb://localhost/my_database', {  useNewUrlParser: true,  useUnifiedTopology: true, }); // Fetch all contacts Contact.find({}, (err, contacts) => {  if (err) {    console.error(err);    return;  }  console.log(contacts); });

In the above example, we first establish a connection to the database using mongoose.connect(). Then, we call the find() method on the Contact model to retrieve all documents from the collection. The first argument to find() is an empty object, which means we want to retrieve all documents from the collection. The second argument is a callback function that will be executed when the query is complete. If an error occurs during the query, we log the error to the console. If the query is successful, we log the retrieved documents to the console.

We can also use various options with the find() method to filter and sort the retrieved documents. Here's an example that shows how to find all contacts whose first name is "John":

javascriptCopy code

// Fetch contacts with first name "John" Contact.find({ firstName: 'John' }, (err, contacts) => {  if (err) {    console.error(err);    return;  }  console.log(contacts); });

In the above example, we pass an object with a firstName property to the find() method. This will retrieve all documents from the collection where the firstName property is equal to "John".

We can also use the sort() method to sort the retrieved documents. Here's an example that shows how to retrieve all contacts and sort them by last name:

javascriptCopy code

// Fetch all contacts and sort by last name Contact.find({})  .sort({ lastName: 'asc' })  .exec((err, contacts) => {    if (err) {      console.error(err);      return;    }    console.log(contacts);  });

In the above example, we call the sort() method on the query object returned by find(). The argument to sort() is an object that specifies the sorting order. In this case, we're sorting by the lastName property in ascending order. Finally, we call the exec() method to execute the query and retrieve the documents. The callback function will be executed when the query is complete, and we log the retrieved documents to the console.

31. My First Express App - Intro To Databases

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support