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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?