Mongodb installation and simple use

Source: Internet
Author: User
Tags install mongodb

Mongodb installation and simple use
Mongodb installation and simple use

First install mongodb

1.: http://www.mongodb.org/downloads

2. decompress the package to the directory you want to install, such as d: \ mongodb.

3. Configure the environment variable, and add the installation directory path: d: \ mongodb \ bin in path;

4. in the mongodb folder you installed, create the folders d: \ mongodb \ db and d: \ mongodb \ log. mongodb. log is used to store the installation db and log files (mongodb. log must check the file attributes to ensure that they are in log format ).

5. We can add the MongDB Service Startup item to the service.
-Install db
Mongod-dbpath "D: \ mongodb \ db"
-Registration Service
Mongod-dbpath "D: \ mongodb \ db"-logpath "D: \ mongodb \ log \ mongodb. log"-install-serviceName

"MongoDB"
After registration, we can open the MongoDB service in the service.

Next, let's talk about the simple use of mongodb.
First enable the Service
Input mongodb in cmd;

-View help
Db. help ();

-Create a database
Use yc

-View the collection under the current database
Show collections;

-Create a set
1. Add data directly to the set. If the set does not exist, it will automatically create
Db. yc. insert ({"_ id": 1001, "name": "yc "});

** In this case, you can use the show collection command to find two sets: ** system. indexes; index set;

2. db. createCollection ("vavy ");

-Delete A set
Db. collection_name.drop ();

-Delete record (document)
Db. collection_name.remove ({condition });

-Add multiple records to the set
Db. collection_name.insert ({"_ id": 1002, "name": "James", "sex": "male"}), ({"_ id": 1003, "name": "Zhang", "sex": "female "});
Db. yc. insert ({"_ id": 1002, "name": "scott", "sex": "M"}), ({"_ id": 1003, "name": "scott", "sex": "F "});

-View data in the Set
Db. collection_name.find ();

* Data Type
Null: {"x": null}
Boolean: {"x": true}
Value: {"x": 3.14} {"x"; 3} NumberInt ("3") NumberLong ("3 ")
Character: {"x": "hello "}
Regular Expression: {"x":/hello/ig}
Array: {"x": [1, 2, 3]}
Embedded document: {"x": {"foo": {bar }}}
Object id: {"x": ObjectId ()}

-If yes, this parameter is added if no. exists.
Db. yc. save ({"_ id": 1004, "name": navy1 });

-Modify
Db. collection_name.update ({condition}, {data to be modified });

Db. yc. update ({"_ id": 1004, "name": navy1 });

Db. navy. insert ({"_ id": 1001, "url": "www.baidu.com", "pageViews": 1 });

-Modifier
$ Inc Add the corresponding value

Db. navy. update ({"_ id": 1001}), {"$ inc": {"pageViews": 1 }}; -Increase the value of the pageViews key in the document with id 1001 by 1.

$ Set
Db. yc. update ({"_ id": 1002 },{ "name": "scott1", "sex": "F "})

db.yc.update({"_id":1002},{"$set":{"name":"scott1","sex":"F"}})

-Convert company into an array
Db. navy. update ({"_ id": 1002 },{ "$ set": {"company": ["yc", "nh", "navy"]})

-Delete key
Db. navy. update ({"_ id": 1002 },{ "$ unset": {"company": 1})-delete the company key in record 1002

-Array Modifier

    db.navy.update({"_id":1002},{"$push":{"company":"sc"}}); $each    db.navy.update({"_id":1002},{"$push":{"company":{"$each":["hg","rc","jm"]}}});

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

    db.navy.update({"_id":1002},{"$push":{"company":{"$each":["yc1","yc2","yc"],"$slice":-10}}});

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

    db.navy.update({"_id":1002},{"$pop":{"company":1}})

$ Pull: delete matched values from the array

    db.navy.update({"_id":1002},{"$pull":{"company":"sc"}})

Db. navy. insert ({
"_ Id": 1005,
"Content": "How about nanhua university ?",
"Comments ":[
{"Comment": "good", "count": 0 },
{"Comment": "good", "count": 0 },
{"Comment": "very good", "count": 0}
]
})

-Access through array subscript
Db. navy. update ({"_ id": 1004 },{ "$ inc": {"comments.1.count": 1 }});

Db. navy. update ({"comments. comment": "good "},
{"Inc": {"comments.. count": 1 }})
Db. navy. update ({"comments. comment": "good "},
{"Set": {"comments .. comment": "very good "}})

-By default, MongoDB only modifies one document at a time. To modify all records that meet the conditions, add the condition {multi: true}
Db. navy. update ({"comments. comment ":" Good "}, {" inc ": {" comments .. count ": 1 }}, {multi: true })
Db. navy. update ({"comments. comment": "Good"}, {"inc": {"comments.. count": 1 }}, false, true)

-Delete
Db. collection_name.remove ({condition });

-Query
Db. yc. find ();-query all

-Paging Query
Db. yc. find (). limit (3)-First 3
Db. yc. find (). limit (3). ship (3)-skip the first three items and query the next three items

-Sort
Db. yc. find (). sort ({"_ id":-1})-1 is in ascending order, and-1 is in descending order.

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.