Connect to MongoDB:
The MONGO 192.168.18.xx:27088/ups-uusername-ppassword--mongo command opens a JavaScript shell. So JS syntax can work in this area.
Help:
Help
Db.help ()
Db.mycoll.help ()
To switch or create a database:
Use TestDB
To create a collection:
Db.createcollection ("mycollection")--See Add option parameter Specify collection options
Users and other basic information inquiries:
Db.adduser (' name ', ' pwd ')--Add or modify user passwords
Db.system.users.find ()--View user list
Db.auth (' name ', ' pwd ')--user authentication
Db.removeuser (' name ')--delete user
Show users--View all users
Show DBS--View all databases
Show Collections-View all collection
Db.printcollectionstats ()--View the status of each collection
Db.copydatabase (' mail_addr ', ' mail_addr_tmp ')--copy Database
Db.mail_addr.drop ()--Delete collection
Db.dropdatabase ()--Delete the current database
Insert:
Db.mycollection.insert ({"A": "S", "SD: 2}")--"_id": This field is the GUID that the database gives us by default to ensure the uniqueness of the data
Or: Db.mycollection.save ({"A": "S", "SD": 2})
var data={"name": "Limin", "Age": $, "Address": "Shenzhen"}
Db.mycollection.insert (data)
Db.mycollection.find ()
Data.name= "YYY"
Db.mycollection.insert (data)
Db.foo.save ({' name ': ' Ysz ', ' address ': {' city ': ' Beijing ', ' Post ': 100096}, ' phone ': [138,139]})--Store nested objects
Db.user_addr.save ({' Uid ': ' [email protected] ', ' Al ': [' [' [email protected] ', ' [email protected] '} ')--store the array object
Delete:
Db.mycollection.remove ({})--delete all data in mycollection
Db.mycollection.remove ({"Name": "Eee"})--delete all data that satisfies the criteria
Update:
Db.mycollection.update ({"name": "www"}, {"name": "Eee"}) the first parameter of the--update method is "condition found" and the second parameter is "updated value" for the overall update (but only one is updated)
Db.mycollection.update ({"name": "www"}, {$inc: {"Age": 10}})--local update, age increased by 10
Db.mycollection.update ({"name": "www"}, {$set: {"Age": 10}})--local update, age changed to 10
Db.mycollection.update ({"name": "www"}, {"name": "Eee"},true)
Db.mycollection.update ({"name": "www"}, {"name": "Eee"},upsert=true)--upsert operation (set the third parameter of update to TRUE), Update if not found according to the conditions, in the database add a
Db.mycollection.update ({"Age": ten}, {$set: {"age": 32}},upsert=true,multi=true)--Bulk update (set to True in the fourth parameter of update), Updates that meet the criteria
Inquire:
Db.mycollection.find () or Db.mycollection.find ({})--Query all
Db.mycollection.find ({"Age": 22})--query for age 22
Db.mycollection.findOne ({"name": "www"})--to find out a piece of data
Db.mycollection.find ({}). Limit (2)--limits the number of data bars to query
Db.users.find (). Skip (3). Limit (5)--Starts with record 3rd (0) and returns 5 records
$where query:
Db.testy.find ({"$where": function () {return this.age==22}})
Or: Db.testy.find ({"$where": "This.age==22"})--Queries with $where clauses are slower than regular queries in speed. Because the document needs to be converted from Bson to a JavaScript object and then run through an expression of "$where"
Relationship query:
"$gt", "$gte", "$lt", "$lte", "$ne", "No Special keywords" (>=, <, <=,! =, =):
Db.mycollection.find ({"Age": {$lt: 22}})
"No keywords", "$or", "$in", "$nin" (and,or,in,notin):
Db.mycollection.find ({"Age": $, "name": "www"})--and
Db.mycollection.find ({$or: [{"Name": "yyy"},{"Age": Ten}]})--or
Db.mycollection.find ({"Age": {$in: [+]}})--in
Db.mycollection.find ({"Name":/^w/})--Using regular expressions
Sort:
Db.mycollection.find ({}). Sort ({"Age":-1})--In descending desc
Db.mycollection.find ({}). Sort ({"Age": 1})--in ascending ASC
Query for child objects:
Db.foo.find ({' address.city ': ' Beijing '})
Aggregate query:
Db.mycollection.count ({}) total number of data bars in--mycollection
Db.mycollection.count ({"Age": 22})--number of data bars that meet the criteria
Db.mycollection.distinct ("Age")--age value of the non-repeating collection
Group Group query:
Db.mycollection.group ({
"Key": {"Age": true},
"Initial": {"Nihao": []},
"$reduce": function (Cur,prev) {
Prev.nihao.push (Cur.name);
}
})
Description
Key: This is the group key, we are here for the age group.
Initial: Each group shares an "initialization function", paying special attention to: Each group, such as the age=20 value of the list share a initial function, AGE=22 also share a initial function. (Nihao, cur, prev are custom, name is the key of the data to be displayed)
$reduce (or reduce): the first parameter of this function is the current document object, the second parameter is the cumulative object of the last function operation, and the first is {"Perosn" in initial: []}. How many documents are there, and how many times $reduce will be called.
Db.mycollection.group ({
"Key": {"Age": true},
"Initial": {"Nihao": []},
"Reduce": function (Cur,prev) {
Prev.nihao.push (Cur.name);
},
"Finalize": function (prev) {
Prev.count=prev.nihao.length;
}
})
Description
Add a Count property to indicate the quantity in each group
Finalize: This is a function, each set of documents after the execution, many will trigger this method, then in each set of collections plus count is its life.
Db.mycollection.group ({
"Key": {"Age": true},
"Initial": {"Nihao": []},
"Reduce": function (Cur,prev) {
Prev.nihao.push (Cur.name);
},
"Finalize": function (prev) {
Prev.count=prev.nihao.length;
},
"condition": {"age": {$lt: 25}}
})
Description
Condition: This is the filter condition. The condition that is not satisfied is filtered out.
Cursor:
(similar to deferred execution)
var cc=db.mycollection.find ({})--Declare a "query structure" CC
Cc.next ()--when needed, next reads a
Cc.foreach (function (x) {print (X.age)})--or read through a For loop
After the cursor is enumerated, the cursor is destroyed and no data is read.
Example:
var cursor = db. C.find ()--defining cursors
while (Cursor.hasnext ()) {
var obj = Cursor.next ();
Print (OBJ.A);
......
}
Index:
Profiling function (explain): Db.mycollection.find ({}). Explain ()
Main Field Explanation:
Cursor: Here is the "basiccursor", what is the meaning of the search here is the "table scan", that is, the order of search, very sad urge ah.
Nscanned: Here is 10w, that is to say the database browsed 10w documents, very scary bar, so that people can't stand ah.
N: Here is 1, which is the final return of 1 documents.
Millis: This is our most most .... Care for something that takes 114 milliseconds altogether.
Db.mycollection.ensureIndex ({"Name": 1})--an index was established on name using Ensureindex. 1 means ascending by name, 1 means descending by name
Db.mycollection.ensureIndex ({"Name": 1},{"unique": true})--Create unique index, duplicate key value cannot be inserted naturally
Db.mycollection.ensureIndex ({"Name": 1, "Age": 1})--Combined index
Db.mycollection.find ({}). Getindexes ()
Db.mycollection.getIndexKeys ()--index information of mycollection collection
Db.mycollection.find ({}). Hint ({"Age": 1})--executed in accordance with your own scheme of violence
Db.mycollection.dropIndexes ("name")--Delete index
MongoDB Operations Command