| Start |
|---|
| Connecting To a Database To connect to a mongo database from node application we require mongoose. Installing Mongoose Go to the toot of your application and install mongoose by Next we connect to the database. |
| Inserting Documents For inserting a new document in the collection, we create a object of the schema. We save it like the following This will insert a new document in the collection |
| Creating New Collection With Mongoose, everything is derived from a Schema. Lets create a schema. Remember methods must be added to the schema before compiling it with mongoose.model() like done above .. |
| Reading Reading Data from the collection is very easy. Getting all data of the collection. Reading data with a condition You can also specify the second parameter as object of what all fields you need Finding one document in a collection. Finding one document in a collection by id . |
| Updating For updating collections and documents we can use any of these methods: Methods
Update()The update() method modifies one or many documents (update parameters) This operation searches the 'lights' collection for a document where UpdateOneThe UpdateOne() method modifies ONE document (update parameters) This operation searches the 'countries' collection for a document where UpdateManyThe UpdateMany() method modifies multible documents (update parameters) This operation updates all documents (in a 'food' collection) where a = Number of matched documents ReplaceOneReplaces the first matching document (replacement document) This example collection called countries contains 3 documents: The following operation replaces the document And returns: The example collection countries now contains: |
| Deleting Deleting documents from a collection in mongoose is done in the following manner. RemarksThese are the basic CRUD operations for using mongo db with nodejs. Question: Are there other ways you can do what is done here ?? Answer : Yes, there are numerous way to do this. Question: Is using mongoose necessary ?? Answer : No. There are other packages available which can help you. Question: Where can I get full documentation of mongoose ?? Answer: Click Here (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference