An open API service indexing awesome lists of open source software.

https://github.com/manni2000/mongoose

Mongoose Documents
https://github.com/manni2000/mongoose

database mongodb mongoose nosql

Last synced: 2 months ago
JSON representation

Mongoose Documents

Awesome Lists containing this project

README

          

# Mongoose doc

Firstly, We will try to understand ..

What is MongoDB?

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.
For more details Documents here!



Now, You will understand something called Mongoose? ...

Mongoose is a Javascript object-oriented programming library that creates a connection between MongoDB and the Node-js JavaScript runtime environment.


The following functions are used on the Document of the Mongoose:

Retrieving: The document is retrieved by different model functions like findOne(), findById().

const doc = MyModel.findById(myid);


Saving: The document is saved by calling the save function. The function is asynchronous and should be awaited.

await doc.save()


Updating using save(): The document can be updated by the save() function as follows:

await MyModel.deleteOne({ _id: doc._id });

doc.firstname = 'Manish';

await doc.save();


Updating using queries: The document can be updated by the queries without calling the save function.

await MyModel.findByIdAndUpdate(myid,{firstname: 'Manish'},function(err, docs){});

Let understand after combinating all above function in one example:


We will be using node.js for this example. Node.js and npm should be installed.

Step 1: Create a folder and initialize it:

npm init


Step 2: Install mongoose in the project.

npm i mongoose

The project structure is as follows:

![image](https://user-images.githubusercontent.com/91480902/200485353-86efe2e4-02c5-4437-af33-5c3f1d160954.png)



Step 3: Create a file called index.js. Inside the index.js, connect to MongoDB. Here the MongoDB Compass is used.

index.js


![image](https://user-images.githubusercontent.com/91480902/200485813-11cbaa8a-8568-449c-9130-8b21b8fb7664.png)


Step 4: Now run the code using the following command in the Terminal/Command Prompt to run the file.

node index.js


OUTPUT:

![image](https://user-images.githubusercontent.com/91480902/200486263-e25b912a-c566-4651-aa46-27f96fbfddc8.png)



And the document in the MongoDB is as follows:

![image](https://user-images.githubusercontent.com/91480902/200486853-461bf734-28a5-430e-982b-9334c848be56.png)


I'm Giving Just Quick Note.

If You like this Repo Give Star Mark.

Thank You for Reading!