MongoDB Database Basic Operations

Source: Internet
Author: User

Generally speaking, there are four kinds of operations involved in MongoDB: adding and deleting.

The Nodejs can be easily and concisely implemented in these operations.

preparatory work:
①, connecting to MongoDB server

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

Server here refers to local (localhost) servers

②, connecting to a database on a server

varnew mongodb.Db(‘users‘, SERVER, {safe:true});

Servers refer to the server on which the database is located, where DB refers to the user database on the server.

③, open the database.
When the above connection is OK, we can open the database. Open in the following way:

DB.open(function(err, db){        if (err) {            // throw err        else {            // 打开成功, db就是打开的数据库,接下来“增删改查”的操作都对这个db来就可以了        }}

Of course, we generally do not directly for a database overall operation, but for one of the collection to operate, then you can:

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

This xcollection points to the collection of the "Userslist" in the DB database above.

However, if this is the first time you are using this collection, you can enter the MONGO command to create a collection called "Userslist", or new and continue with the following methods:

db.createCollection(‘userslist‘, {safe:truefunction(err, Xcollection){    //同样在这里可以操作Xcollection,和上面意义是一样的。}

Using Db.createcollection (), if the collection already exists, then it is just open.

++++++++++++++++++++++++++++++++++++++++++
1, Increase
Add a User object (such as {"A": "B"} to collection) and you can do this:

collection.insert(user, {safe:truefunction(err, result){    // result是一个对象,表示插入数据的结果})

2. Check
In collection, querying a User object can do this:

collection.find(user).toArray(function(err, items) {    //items是一个对象数组,从0开始编号,items.length表示查找到的对象个数})

However, it is important to note that all the results of the query are Userx, not necessarily identical to the user, as long as the Userx contains all of the user's key-value pairs and the values are equal, then Userx is included in items[].

3, change
Cond

4. By deleting
To be continued, too

Eh, is the most obvious content, more comprehensive or look at the official documents it.

MongoDB Database Basic Operations

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.