10 minutes to teach you how to read MongoDB's NPM package

Source: Internet
Author: User
Tags api manual mongoclient

Recently started to contact with node. js, in a blog, in the learning process, it must be a lot of contact with NPM package, mainly learning to use its API. What is written now is about the use of MongoDB. First MongoDB This package is used to provide the connection and operation Mongdb This database package, if your background database is using MongoDB then we will use this package.

First we search for this package in NPM , which is the first one

We'll go in and see his specific information, we'll look directly at its API manual.

The document's home page then describes the approximate role of some of the resource files (JS files): http://mongodb.github.io/node-mongodb-native/2.1/api/

For example:

Here the connection.js is mainly used for the operation of the database, if we want to find the appropriate API must be found in this file. It also indicates that the collection instance cannot be created directly (the black bold part).

To manipulate the database, we first have to connect to the database, there are two ways:

The first creates an mongoclient instance that connects to the database via a URL

var Mongoclient = require (' MongoDB '). Mongoclient, Mongoclient.connect (function(err, db) {  //Todo}

Here, one of the key points when looking at a document is to look at the description of the callback function. Err represents the connection time error, if the connection to the database is null, and if it fails, err will not be null, so usually the callback function inside we will make err judgment, to do the corresponding processing. DB is the connected database.

The second way: not using mongoclient instances to create, there are many ways, here only one

Connecting the database in this way should be appropriate for a single database server, and bold text in bold indicates that the server should not be used when we are using mongoclient to connect to the database.

Now that we have connected to the database, then the next is to operate the database, since the operation of the database, the first step should open the database, we will certainly associate with the open Word, according to previous experience. Nice connection. The database is indeed the Open method, which exists in our db.js and is a method of db instances. If sometimes we don't know which class we need, we can search in the search bar on the left.

Mongodb.open ( err, db) {//MongoDB for connected database    if  (err) {         //todo     }


Look at the use of this API, be sure to see what the corresponding callback function means.

The callback function explains the two parameters, the parameters are very simple, and there is no point here. Of course, if the database opened successfully and other operations resulted in an error causing the failure, the database must be closed first, so the Close method is also in the DB instance.

Now that you have opened the database, you can implement the operational database, the relevant methods of manipulating the database, in the collection class mentioned above. The most commonly used is to delete and change, if the English offensive, then look at this bar:


This article is mainly about how to learn NPM packages, how to look at the API. Of course, I also have a little experience in learning:

1 English is very important

2 official website is very important

3 callback functions are important, especially the way the code is organized, and I don't understand well enough now.

10 minutes to teach you how to read MongoDB's NPM package

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.