MongoDB is a code-sourced, cross-platform, document-oriented database program developed by Alfons Kemper. Distributed as a NoSQL database program, MongoDB uses JSON-like data with optional schemas
What’s special about the Mongoose library when using MongoDB
What is so special about the Mongoose library when working with MongoDB
Mongoose provides options before and after saving work on data structures. This makes it easy to define hooks and custom functions like read/write efficiency. You can also define custom actions that trigger certain parameters (or data).
In the above code, we’re using the Mongodb npm package which provides a pure implementation of interacting with the MongoDB database.
In the above code, we are inserting a new document into the user’s collection.
If we want to prevent the user from entering bad data, we need to add validation to check for each input field. So you might need to trim the value and convert it to lowercase as the user might enter the data in uppercase, etc. before saving the data into the database.
Mongoose fixes all of these issues by providing in-built basic validations. It also allows us to write our own validation if required. So makes it difficult to mess with the data to be inserted.
Example of inserting a single document:
db.collection.insertOne( { name: “John”, age: 30, city: “New York” }
db.collection.insertMany([ { name: “John”, age: 30, city: “New York” }, { name: “Jane”, age: 25, city: “Los Angeles” }, { name: “Bob”, age: 40, city: “Chicago”
MongoDB is a persistent document-oriented database used to store and process data in the form of documents. As with other database management systems, MongoDB allows you to manage and interact with data through four fundamental types of data operations:
CREATE –
These steps focus on creating data in MongoDB to practice reading, modifying, and deleting data in the next steps of this guide.
Suppose you use MongoDB to create and manage catalogs of world-famous history. The book will store information such as the name, country, city, and region of each monument.
MongoDB will return the following result:
READ –
Now that you have some data stored in your script, you can ask the datastore to store the data and read its data. This step explains how to query all records in the database first, and then how to use filters to narrow the list of records.
MongoDB will return the following result:
UPDATE –
Similar to the insertOne() and insertMany() methods, MongoDB provides methods that allow you to update one or more record information at once.
An important difference between these updates is that when creating a new document, you only need to pass the data file as a parameter. To update an existing file in a collection, you must pass a custom command that will update the file.
MongoDB will return the following result:
Delete –
Like Mongo’s update and insert operations, it has a deleteOne() method that only deletes the first record that matches the filter query, and a deleteMany() method that deletes multiple records at once.
MongoDB will return the following result: