Apsaradb for MongoDB basic knowledge

Source: Internet
Author: User

Apsaradb for MongoDB basic knowledge
1. Introduction:

MongoDB is a distributed file storage-based database. Written in C ++. It is designed to provide scalable, high-performance data storage solutions for WEB applications.
MongoDB is a product between relational databases and non-relational databases. It has the most abundant functions and features like relational databases. The supported data structure is very loose and is similar to the json bson format, so it can store more complex data types. The biggest feature of Mongo is that it supports a very powerful query language. Its syntax is somewhat similar to an Object-Oriented Query Language. It can almost implement most of the functions similar to single-table queries in relational databases, it also supports data indexing.

2. features:

It features high performance, ease of deployment, and ease of use, making it easy to store data. Features:
It is designed for centralized storage and is easy to store object-type data.
Free mode, support for dynamic query, support for full indexing, including internal objects. Query is supported. Supports replication and fault recovery. Use efficient binary data storage, including large objects (such as videos ). Automatic fragment processing to support scalability at the cloud computing level. Supports RUBY, PYTHON, JAVA, C ++, PHP, C #, and other languages. The file storage format is BSON (a json extension ). It can be accessed through the network.
For details, refer to Baidu Encyclopedia:
Http://baike.baidu.com/subview/3385614/9338179.htm#viewPageContent

3. Use:

(1) installation: https://www.mongodb.org/downloads. download the development environment package of Alibaba Cloud and install it directly;
(2) create a warehouse, log, and create a service: create two folders db and log under the installation directory; enter the following command in cmd to create a warehouse, log, and create a service:
Mongod-dbpath "D: \ study soft \ MongoDB \ db"-logpath "D: \ study soft \ MongoDB \ log \ mongodb. log"-install-serviceName "MongoDB"
(3) Use the volume vue tool:
After downloading and installing (not introduced), to create a connection, you only need to write the Name (connection Name) and Server (Address: localhost or 127.0.0.1:

After creation

(4) Basic commands:
-- Create a set:
Db. createCollection ("Set Name ");
-- Delete a set:
Db. Set Name. drop ();
-- Switch set:
Use set name;
-- Add record:
Db. Set name. insert ({"_ id": 1001, "name": "yyy", "sex": "F "});
-- Add multiple records to the set
Db. set Name. insert ([{"_ id": 1003, "name": "ss days", "sex": "male"}, {"_ id": 1004, "name": "tt", "sex": "male"}]);
-- View data in the Set:

Db. set Name. find () db. set Name. findOne () db. set Name. find ({"_ id": 1001}) --- where key = val db. set Name. find ({"_ id": {"gt": 1001 }}) --- where key> val db. set Name. find ({"_ id": {"$ lte": 1001 }}) --- where key <= val db. set Name. find ({"_ id": {"$ ne": 1001}) --- where key! = Val

-- $ In ..... Medium

Db. Set Name. find ({"_ id": {"$ in": [1003,]})

-- $ Or

Db. Set name. find ({"$ or": [{"_ id": 1001 },{ "name": "haha"}]})

-- $ And or and .... Or ..... (Gender: male and id: 1009 or name: yc)

Db. zy. find ({"sex": "male", "$ or": [{"_ id": 1009 },{ "name": "yc"}]})

-- If yes, it is modified. If no, it is added.
Db. Set name. save ({"_ id": 1003, "name": "haha "});
-- Modify
Db. Set Name. update ({condition}, {data to be modified });

-- Modifier
$ Inc Add the corresponding value

Db. zy. insert ({"_ id": 1004, "url": "www.baidu.com", "pageViews": 1}); db. zy. update ({"_ id": 1004 },{ "$ inc": {"pageViews": 1 }}); -- increase the value of the pageViews key in the document with id 1004 by 1

$ Set modify the corresponding value

Db. zy. update ({"_ id": 1002 },{ "name": "", "sex": "female"}) db. zy. update ({"_ id": 1002 },{ "$ set": {"name": "", "sex": "male "}})

-- Convert company into an array

    db.zy.insert({"_id":1005,"company":"yc","url":"www.baidu.com","pageViews":1});    db.zy.update({"_id":1005},{"$set":{"company":["yc","zy","nh"]}})

-Deletion key:

    db.zy.update({"_id":1005},{"$unset":{"company":1}})

-Array modifier:
$ Push adds a value to the array. duplicate values may appear.

     db.zy.update({"_id":1005},{"$push":{"company":"hhh"}})

$ Add an each loop

    db.zy.update({"_id":1005},{"$push":{"company":{"$each":["aa","bb","cc"]}}})

$ Slice specifies the maximum length. Its value must be negative, indicating that the last n values are retained.

    db.zy.update({"_id":1005},{"$push":{"company":{"$each":["aa1","bb2","cc3"],"$slice":-10}}})

$ Pop: deletes an element key from the array: 1 from the end of the data:-1 from the header

     db.zy.update({"_id":1005},{"$pop":{"company":1}})

$ Pull: delete matched values from the array

     db.zy.update({"_id":1005},{"$pull":{"company":"nh"}})

-- Loop

 for( i=0;i<10;i++){        db.zy.insert({"_id":i,"name":i})    }  

Note: When _ id is not inserted, mongodb automatically allocates an ObjectId.
-- Cursor

    var cursor =db.zy.find();    var obj;    while(cursor .hasNext()){        obj=cursor.next();        print(obj);    };    var cursor =db.zy.find();    cursor .forEach(function(x){        print(x);    })

-Paging Query

Db. zy. find (). limit (3); --- query the first three databases. zy. find (). limit (3 ). skip (3); --- skip the first 3 and check the next 3

-Sort

Db. zy. find (). sort ({"_ id":-1}) --- 1 is ascending-1 is descending db. zy. find (). sort ({"_ id":-1, "name": 1}) --- sort by the second if the first one is the same

Eg:

Db. zy. insert ({"_ id": 1006, "content": "What is the weather like today ?? "," Comments ": [{" comment ":" Good "," count ": 0 },{" comment ":" Good "," count ": 0 }, {"comment": "very good", "count": 0},]})

-- Access through array subscript

Db. zy. update ({"_ id": 1006 },{ "$ inc": {"comments.1.count": 1}) db. zy. update ({"comments. comment ":" Good "}, {" $ inc ": {" comments. $. count ": 1}) ---- match all the" good "and make the corresponding count + 1 db. zy. update ({"comments. comment ":" very good "}, {" $ set ": {" comments. $. comment ":" super good "}) ---- match all" very good "and change the corresponding value to" super good ".

-- By default, only one document is modified at a time for MongoDB. To modify Multiple matching records, add the condition {multi: true}

Db. zy. update ({"comments. comment ":" Good "}, {" $ inc ": {" comments. $. count ": 1 }}, {multi: true}) db. zy. update ({"comments. comment ":" Good "}, {" $ inc ": {" comments. $. count ": 1 }}, false, true)

(5). Data Type:
Null: {"x": null}
Boolean: {"x": true}
Value: {"x": 3.14} {"x": 3} NumberInt ("3") NumberLong ("3 ")
String: {"x": "hello "}
Date: {"x": new Date ()}
Regular Expression: {"x":/hello/ig}
Array: {"x": [1, 2, 3]}
Embedded document: {"x": {"foo": {bar }}}
Object id: {"x": ObjectId ()}
Code: {"x": function (){}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.