function MongoDB on the CENTOS7

Source: Internet
Author: User
Tags install mongodb yum install mongodb

Installing the service side

Yum Install Mongodb-server

Installing the Client

Yum Install MongoDB

Version
MONGO--version

Is MongoDB installed?
Which Mongod
History | grep MONGO

Mkdir-p/data/db/

Start

Mongod--dbpath=/data/db--port=27017

Off, also available for kill

Mongod--shutdown

Add log

Mongod--dbpath=/data/db--port=27017--fork--logpath=/var/log/mongod.log

View Logs

Tail-f/var/log/mongod.log

Connecting clients

Mongo

Display Database

>show DBS;

Create a database

>use Part9;

Insert Document

>db.users.insert ({"username": "Sid"});

Show collection

> Show Collections
System.indexes
Users

Querying a document in a collection
> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
> Db.users.insert ({"username": "Zoe", "Group": "Reporter"});
Writeresult ({"ninserted": 1})

> Db.users.insert ({"username": "Zoe", "Group": "Reporter"});
Writeresult ({"ninserted": 1})
> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "Group": "Reporter"}

Query number of documents
> Db.users.find (). Count ();
2
> Db.users.insert ({"username": "Zoe", "Group": "Programmer"});
Writeresult ({"ninserted": 1})
> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "Group": "Reporter"}
{"_id": ObjectId ("5654 6d68ac40378400147959 ")," username ":" Zoe "," Group ":" Programmer "}
> Db.users.find (). Count ();
3
> Db.users.find ({"_id": ObjectId ("56546ce3ac40378400147958")});
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "Group": "Reporter"}

Update the document, but only one line of the document is modified
> db.users.update ({"username": "Zoe"}, {$set: {"group": "Writer"}});
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "group": "Writer"}
{"_id": ObjectId ("56546d68ac40378400147959"), "username": "Zoe", "Group": "Programmer"}

Update all documents that meet the criteria, multi:true
> db.users.update ({"username": "Zoe"}, {$set: {"group": "Writer"}}, {multi:true});
Writeresult ({"nmatched": 2, "nupserted": 0, "nmodified": 1})
> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "group": "Writer"}
{"_id": ObjectId ("56546d68ac40378400147959"), "username": "Zoe", "group": "Writer"}

Use Save () to modify
>db.users.save ({"_id": ObjectId ("56546d68ac40378400147959"), "group": "Reporter"});

> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "group": "Writer"}
{"_id": ObjectId ("56546d68ac40378400147959"), "group": "Reporter"}

Update () has three parameters, save () has only one parameter, the difference between the two, note

Delete a document from the collection remove ()

First construct a collection as follows

> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "group": "Writer"}
{"_id": ObjectId ("56546d68ac40378400147959"), "group": "Reporter"}
{"_id": ObjectId ("56547d15ac4037840014795a"), "group": "Reporter"}
{"_id": ObjectId ("56547d18ac4037840014795b"), "group": "Reporter"}
{"_id": ObjectId ("56547d1bac4037840014795c"), "group": "Reporter"}
{"_id": ObjectId ("56547d1cac4037840014795d"), "group": "Reporter"}

5 Documents deleted

> Db.users.remove ({"Group": "Reporter"});
Writeresult ({"Nremoved": 5})
> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "group": "Writer"}

Re-construct a collection

> Db.users.find ();
{"_id": ObjectId ("565468b4ac40378400147957"), "username": "Sid"}
{"_id": ObjectId ("56546ce3ac40378400147958"), "username": "Zoe", "group": "Writer"}
{"_id": ObjectId ("56547e2bac4037840014795e"), "group": "Reporter"}
{"_id": ObjectId ("56547e2dac4037840014795f"), "group": "Reporter"}
{"_id": ObjectId ("56547e2fac40378400147960"), "group": "Reporter"}
{"_id": ObjectId ("56547e30ac40378400147961"), "group": "Reporter"}

Add the second parameter in remove () to delete only the first document in the collection that satisfies the condition

> Db.users.remove ({"Group": "Reporter"}, true);
Writeresult ({"nremoved": 1})

Delete all the documents

> Db.users.remove ({});
Writeresult ({"Nremoved": 5})
> Db.users.find ();

Delete all documents and indexes in the collection

> Db.users.drop ();
True

function MongoDB on the CENTOS7

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.