Mongo with mongoose

How to create document using mongoose in express-node app


Mongoose is MongoDB driver module which provide REST features to build faster MongoDB apps with Nodejs.

MongoDB

MongoDB is a NoSQL database which store data as JSON document and collections. In JavaScript applications we can utilize the mongoose driver to perform Mongo operations. First up all we need to install mongoose to your project and then import the module.

npm install mongoose

Saving the document

mongoose uses the modal class to perform REST operations. If you are not familiar have a look at the mongoose modal post given below

Tag.create({
tagName: tagNa}, function(error, stag) {
if (error) return handleError(error)
 console.log(new tag saved : ${stag.tagName});
})

The Tag is the Modal and the create method is used to save the document to database and it also use a callback function to return the result.

We can also use an asynchronous method find documents in Mongoose as follows

const tag=new Tag({tagName:'SQL'})
await tag.save()

That’s it. Following mongoose posts deserve your attention

Published by

Manoj

A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.