CoffeeScript MongoDB - CoffeeScript

What is CoffeeScript MongoDB?

MongoDB is a cross-platform, record-oriented database that provides, high performance, excessive availability, and easy scalability. MongoDB works on the idea of series and record. For more statistics examine our MongoDB educational.

In this chapter, you will learn how to talk with MongoDB database using CoffeeScript.

Installation

The MongoDB database can be included with CoffeeScript the usage of Node.js 2.0 driver of MongoDB. first of all you want to put in MongoDB to your system, via referring the surroundings chapter of our MongoDB tutorial.

After installing MongoDB efficiently browse via its bin folder (if you haven't set the path) and begin the MongoDB service as shown under.

Finally install MongoDB driver and it's dependencies by executing the following NPM command in the command prompt.

Connecting to MongoDB

in order to connect with MongoDB, to begin with, create MongoClient using this, invoke the connect() function. This function accepts URL and a callback function as parameters.

Following CoffeeScript code shows how to connect to MongoDB server. If the MongoDB server is running on your machine this program establishes a connection to the server.

Save the above code in a file with name connect_db.coffee and execute it as shown below. If database is successfully created then it will give following message.

creating a collection

a collection in MongoDB holds the documents we store in it. you can create a set via the usage of the collection() characteristic. This function accepts a string argument that represents the call of the collection we need to create.

Following CoffeeScript code indicates the way to create a collection in MongoDB. In the case of any errors, they'll be displayed at the console.

Save the above code in a file with name create_collection.coffee and execute it as shown below. If the collection is created successfully then it will give following message

Inserting documents

you may insert files into a group in MongoDB you want to invoke a function named insert() by using passing the list of files which might be needed to be inserted, as parameters.

Following CoffeeScript code suggests a way insert files into a collection named My_collection. In the case of any errors, they may be displayed at the console.

Save the above code in a file with name insert_documents.coffee and execute it as shown below. If the documents are inserted successfully then it gives following message

Reading documents

You could retrieve the documents which might be stored in MongoDB the use of a function named find(). The following CoffeeScript code shows the way to retrieve the data which might be stored in MongoDB.

Save the above code in a file with name read_documents.coffee and execute it as shown below. This programs retrieves the required document in the specified collection and displays it as shown below.

You can also read all the documents existing in a particular collection by executing the find() function with out passing any arguments to it as shown below.

Save the above code in a file with name read_all_documents.coffee and execute it as shown below. this programs retrieves all the documents in the specified collection and displays it as shown below.

Updating Documents

You can update the documents that are stored in MongoDB using a function named update(). Following CoffeeScript code shows how to update the records that are stored in MongoDB.

This program updates the city of the employee named Ram from Hyderabad to Delhi.

Save the above code in a file with name update_documents.coffee and execute it as shown below. this programs retrieves the documents in the specified collection and displays it as shown below.


Document updated
After updating, if you execute the read_documents.coffee program, then you will observe that the city name of the person named Ram is updated from Hyderabad to Delhi.

Deleting Documents

You can delete all the documents from the collection using the remove() function. Following CoffeeScript code shows how to delete all the records that are stored in MongoDB.


Save the above code in a file with name delete_documents.coffee and execute it as shown below. this programs removes all the documents in the specified collection displaying the following messages.


Document deleted

After deleting, if you execute the read_documents.coffee program, then you will get an empty collection as shown below.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics