Mongodb basic operations

Source: Internet
Author: User
Tags mongodb server
Generally, there are four operations involved in mongodb: add, delete, query, and modify. Nodejs can easily and concisely implement these operations. Preparations: ①. Connect to the mongodb Server varSERVERnewmongodb. SERVER (localhost, 27017, {auto_reconnect: true}). Here, the Server refers to the local SERVER (localhost ).

Generally, there are four operations involved in mongodb: add, delete, query, and modify. Nodejs can easily and concisely implement these operations. Preparations: ① connect to the mongodb SERVER var Server = new mongodb. SERVER ('localhost', 27017, {auto_reconnect: true}); here SERVER refers to the local Server (localhost)

Generally, there are four operations involved in mongodb: add, delete, query, and modify.

Nodejs can easily and concisely implement these operations.

Preparations:
① Connect to the mongodb Server

var SERVER  = new mongodb.Server('localhost', 27017, {auto_reconnect:true});

Here, the SERVER refers to the local (localhost) SERVER.

② Connect to the database on the server

var DB = new mongodb.Db('users', SERVER, {safe:true});

SERVER refers to the SERVER where the above database is located, and DB refers to the user database on the SERVER.

③ Open the database.
After the above connection is complete, we can open the database. The method is as follows:

DB. open (function (err, db) {if (err) {// throw err} else {// open successfully, db is the opened database, the next "add, delete, modify, and query" operations will all be performed on this db }}

Of course, we generally do not directly perform operations on a database as a whole, but operate on a collection, so we can do this:

var Xcollection = db.collection("userslist");

In this way, Xcollection points to the collection "userslist" in the above db database.

However, if this collection is used for the first time, you can enter the mongo command to create a collection named "userslist", or use the following method to create and continue the operation:

Db. createCollection ('userlist', {safe: true}, function (err, Xcollection) {// you can operate Xcollection in the same way as above .}

If db. createCollection () is used, if the collection already exists, it will only be opened.

++ ++
1. Add
Add a user object to the collection (in the format of {"a": "B"}). You can do this:

Collection. insert (user, {safe: true}, function (err, result) {// result is an object, indicating the result of data insertion })

2. Query
You can query a user object in collection as follows:

Collection. find (user). toArray (function (err, items) {// items is an array of objects, numbered from 0, and items. length indicates the number of objects found })

However, it should be noted that all the results of the query, userX, are not necessarily exactly the same as the user, as long as userX contains all the key-value pairs of the user and the values are equal, then userX will be included in items.

3. Change
To be continued

4. Delete
To be continued, too

Ah, I am talking about the most simple content. For more information, see the official documentation.

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.