A. Installation
Mongo document: https://docs.mongodb.com/v3.6/administration/install-enterprise-linux/
Linux mongo's profile in general: /etc/mongod.conf
sudo apt-get install mongodb
II. Start mongodb
# Start sudo Service Start the mongod # closed sudo Service the mongod STOP # restart sudo service mongod restart
III. Create a user name and password
1. Create an administrator account
mongo use admin db.createUser( { user: "adminUserName", pwd: "adminPassWord", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
2. Create a common account
mongo use test db.createUser( { user: "simpleUserName", pwd: "simplePassWord", roles: [ { role: "readWrite", db: "test" }, { role: "read", db: "test2" } ] } )
# Permissions: read-write database test, read-only database test2.
IV. Connection
# Local connection mongo # connection username password mongo mongo -u " the User " -p " password " # connection to the remote mongo mongo ip # connection to the remote username password specified database mongo 1 . Username and password directly connected mongo ip / DB -u ' User ' -p ' password ' Example: Mongo 127.0.0.1/admin -u " the root " -p " the root " 2 . connect the remote client during authentication Mongo IP use ADMIN db.auth ( " adminUser ", "adminPass")
V. Basic Operations
1. Common Operation
# View all databases Show dbs # into the test database is created not use test # Create a table db.createCollection ( " Reviews " ) # under the table to see test Show the Tables
2. Basic Query
# View test strip total number of reviews under the table db.reviews.count () or db.reviews.find (). COUNT () # before the query 10 db.reviews.find (). Limit (10 ) # before skipping 10, query 10 . db.reviews.find () Skip (10) .limit (10 ) # query data _id 1 db.reviews.find ({ " _id " : 1})
3. Insert Data
db.COLLECTION_NAME.insert(document)
# Insert a data table reviews db.reviews.insert ({ " the _id " :. 1 }) Results: WriteResult ({ " nInserted " :. 1 }) # insert multiple db.reviews.insert ([{ " the _id " : 2} , { " the _id " :. 3 }]) results: BulkWriteResult ({ " writeErrors " : [], " writeConcernErrors " : [], " nInserted " : 2 , " nUpserted " : 0, "nMatched" : 0, " nModified " : 0, " nRemoved " : 0, " upserted " : [] }) # Note _id inserted already exists, the error will be
4. Delete
If your MongoDB version 2.6 later, the syntax is as follows:
db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> } )
Parameter Description:
- Query :( Optional) condition for the document to delete.
- justone : (optional) If true or 1, only delete a document, if you do not set this parameter, or use the default value is false, all documents matching conditions are deleted.
- writeConcern :( optional) throw-level exception.
# Delete databases, caution use Test db.dropDatabase () # remove reviews table db.reviews.drop () # data deletion reviews qualifying db.reviews.remove ({ " the _id " :. 1 }) # delete only the first a qualified data db.reviews.remove ({ " _id " : 1}, 1 ) # delete all data db.reviews.remove ({})
5. Update Data
update () method is used to update the existing document. Syntax is as follows:
db.collection.update( <query>, <update>, { upsert: <boolean>, multi: <boolean>, writeConcern: <document> } )
Parameter Description:
- Query : query update, similar to the back where within sql update query.
- Update : Update objects and some of the newer operators (e.g., $, $ inc is ...) and the like, may be understood as a set behind the sql update query
- upsert : Optional, this parameter mean, if there is no record update, whether to insert objNew, true inserted, the default is false, not inserted.
- Multi : Optional, mongodb default is false, only updating the first record found, if this parameter is true, according to the conditions put many records to check out all the updates.
- writeConcern : Optional, throw-level exception.
# Update _id: data updates status 3. 1 db.reviews.update ({ " the _id " :. 3}, {$ SET: { " Status " :. 1 }}) Results: WriteResult ({ " nMatched " :. 1, " nUpserted " : 0, " nModified " :}. 1)
If wrong, please share