MongoDB installation and simple use

Source: Internet
Author: User
Tags delete key install mongodb modifier

MongoDB installation and simple use

First install MongoDB

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

2. Unzip to the directory you want to install, such as D:\mongodb

3. Configure the environment variable, the installed directory Path:d:\mongodb\bin added in path;

4. In the MongoDB folder you installed, create folders D:\mongodb\db and D:\mongodb\log.mongodb.log respectively to store the installation DB and journal files (mongodb.log must view the file attributes, guaranteed to be Log format).

5. We can add Mongdb service startup items 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"
Once the registration is complete, we can enter the service to open the MongoDB service.

below I say MongoDB simple to use
Start the service first
Enter MongoDB in CMD;

– View Help
Db.help ();

– Create a library
Use YC

– View the lower collection of the current library
Show collections;

– Create a Collection
1. Add data directly to the collection, and if the collection does not exist, it is automatically created
Db.yc.insert ({"_id": 1001, "name": "YC"});

**此时使用命令show collection会发现有两个集合**    system.indexes;索引集合;

2.db.createcollection ("Vavy");

– Delete Collection
Db.collection_name.drop ();

– Delete records (documents)
Db.collection_name.remove ({condition});

– add more than one record to the collection
Db.collection_name.insert ({"_id": 1002, "name": "Xiaoming", "Sex": "Male"}), ({"_id": 1003, "name": "Xiao Zhang", "Sex": "Female"});
Db.yc.insert ({"_id": 1002, "name": "Scott", "Sex": "M"}), ({"_id": 1003, "name": "Scott", "Sex": "F"});

– View the data in the collection
Db.collection_name.find ();

* Data Type
Null: {"x": null}
Boolean: {"X": True}
Value: {"X": 3.14} {"X"; 3} numberint ("3") Numberlong ("3")
Characters: {"x": "Hello"}
Regular expression: {"X":/hello/ig}
Array: {"x": [All-in-all]}
Inline document: {"X": {"foo": {Bar}}}
Object ID: {"x": ObjectId ()}

– If it exists, it will be added if it does not exist.
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 increase the corresponding value

Db.navy.update ({"_id": 1001}), {"$inc": {"pageviews": 1}};– adds 1 to the value of the Pageviews key in the document with ID 1001

$set
Db.yc.update ({"_id": 1002},{"name": "Scott1", "Sex": "F"})

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

– Turn 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, which means that the last N values are preserved

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

$pop removes an element from the array key:1 starts at the end of the data key:-1 from the head
Began

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

$pull remove a matching value from the array

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

Db.navy.insert ({
"_id": 1005,
"Content": "What about Nanhua University?",
"Comments": [
{"comment": "Good", "Count": 0},
{"comment": "Very good", "Count": 0},
{"comment": "Very good", "Count": 0}
]
})

– Access by 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 ":" Very Good "},
{" set ": {" comments. . Comment ":" Very Good "}})

–mongodb default to modify only one document at a time, if you need to modify all records that meet the criteria, you need to add the condition {multi:true} later
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});

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MongoDB installation and simple use

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.