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