Basic database commands common database commands 1. Help Command Prompt helpdb. help (); db. yourColl. help (); db. youColl. find (). help (); rs. help (); 2. Switch to create database useyourDB. When a set (table) is created, the current database is automatically created. 3. query all databases. 4. Delete
Basic database commands common database commands 1. Help Command display command prompt help db. help (); db. yourColl. help (); db. youColl. find (). help (); rs. help (); 2. Switch to/create database use yourDB; When a set (table) is created, the current database is automatically created. 3. query all databases. 4. Delete
Basic database commands common database commands
1. Help to view command prompts
Help
Db. help ();
Db. yourColl. help ();
Db. youColl. find (). help ();
Rs. help ();
2. Switch to/create a database
Use yourDB; when you create a set (table), the current database is automatically created.
3. query all databases
Show dbs;
4. Delete the currently used database
Db. dropDatabase ();
5. clone a database from a specified host
Db. cloneDatabase ("127.0.0.1"); clone the database data on the specified machine to the current database
6. Copy the specified database data from the specified machine to a database
Db. copyDatabase ("mydb", "temp", "127.0.0.1"); copy the data of mydb on the local machine to the temp database.
7. Fix the current database
Db. repairDatabase ();
8. view the currently used database
Db. getName ();
Db; the db and getName methods have the same effect. You can query the currently used database.
9. display the current db status
Db. stats ();
10. Current db version
Db. version ();
11. view the address of the linked machine of the current db
Db. getMongo ();
Collection aggregation set
1. Create an aggregation set (table)
Db. createCollection ("collName", {size: 20, capped: 5, max: 100 });
2. Get the clustering set (table) with the specified name)
Db. getCollection ("account ");
3. obtain all the aggregation sets of the current db.
Db. getCollectionNames ();
4. display the status of all clustered indexes of the current db
Db. printCollectionStats ();
User-related
1. Add a user
Db. addUser ("name ");
Db. addUser ("userName", "pwd123", true); add a user, set a password, and check whether the user is read-only
2. database authentication and security mode
Db. auth ("userName", "123123 ");
3. display all current users
Show users;
4. delete a user
Db. removeUser ("userName ");
Error message operation
1. query the previous error message db. getPrevError (); 2. Clear the error record db. resetError ();
View basic information about a collection
1. view the help database. yourColl. help (); 2. query the number of data entries in the current set. yourColl. count (); 3. view the data space size db. userInfo. dataSize (); 4. Obtain the db where the current collection is located. userInfo. getDB (); 5. Get the status db of the current cluster. userInfo. stats (); 6. Get the total size of the clustered set db. userInfo. totalSize (); 7. Size of the storage space of the aggregation set db. userInfo. storageSize (); 8. Shard version information db. userInfo. getShardVersion () 9. Rename the database for the clustering set. userInfo. renameCollection ("users"); rename userInfo to users10, delete the current collection db. userInfo. drop ();
Index operations
1. Create an index database. userInfo. ensureIndex ({name: 1}); db. userInfo. ensureIndex ({name: 1, ts:-1}); 2. query all indexes in the current clustered set. userInfo. getIndexes (); 3. view the total index record size db. userInfo. totalIndexSize (); 4. Read all index information db of the current set. users. reIndex (); 5. Delete the specified index database. users. dropIndex ("name_1"); 6. Delete All index databases. users. dropIndexes ();
Query operations
Query database operation SQL under Mongodb-SpringMvc
Http://blog.csdn.net/xiaohulunb/article/details/27828381
1. query all
> db.foo.find(){ "_id" : ObjectId("5389aa1df06b88aaa313746a"), "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "_id" : ObjectId("5389aaa4afce65313a5614f7"), "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "_id" : ObjectId("5389aabaafce65313a5614f8"), "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "_id" : ObjectId("5389aac5afce65313a5614f9"), "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
2. Display specified Columns
The first {} puts the where condition, the second {} specifies which columns are displayed and not displayed (0 indicates not displayed> 0 indicates display)
The following example uses {'_ id': 0} to hide the' _ id column 'by default to reduce the display volume.
> db.foo.find({},{'_id':0,'name':1,'user':1}){ "name" : "yiwa", "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "user" : { "phone" : [ 63, 137 ] } }
3. Use and
# The name is yiwa and the age is 25
> db.foo.find({'name':'yiwa','age':25},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
4. Use or
# The name is yiwa or the age is 75
> db.foo.find({'$or':[{'name':'yiwa'},{'age':75}]},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
5. Use the <, <=,>, >=( $ lt, $ lte, $ gt, $ gte) operation, modulo operation $ mod
#15 <= x <= 75 years old
> db.foo.find({'age':{'$gte':15,'$lte':75}},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
# Modulo result of age % 3 = 1
> db.foo.find({'age':{'$mod':[3,1]}},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
6. Use in, not in ($ in, $ nin)
# The name is not siwa and the age range is [15, 25, 85]
> db.foo.find({'name':{'$nin':['siwa']},'age':{'$in':[15,25,85]}},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
7. Match null operation
# The name is null.
> db.foo.find({'name':null},{'_id':0})>
8. Use like (mongoDB supports regular expressions)
# Name like % iwa % # name like yi %
> db.foo.find({'name':/iwa/},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.find({'name':/^yi/},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }
9. query using distinct and count
> db.foo.distinct('name')[ "yiwa", "erwa", "sanwa", "siwa" ]> db.foo.count()4
# Use distinct in combination with conditions for sorting
> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 95, "user" : { "phone" : [ 123, 133, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 133, 137, 186 ] } }> db.foo.distinct("age",{'user.phone':{'$in':[63,65,186]}}).sort({'age':1})[ 25, 85, 95 ]> db.foo.distinct("age",{'user.phone':{'$in':[63,65,186]}}).sort({'age':-1})[ 25, 85, 95 ]> db.foo.distinct("age",{'user.phone':{'$in':[63,65,186]}})[ 25, 95, 85 ]
Question to be answered :? Why are age:-1 and age: 1 results the same?
10. array query (mongoDB exclusive) (all, size)
# The phone number contains 186
> db.foo.find({'user.phone':186},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }
# The phone number contains 188,186
> db.foo.find({'user.phone':{'$all':[188,186]}},{'_id':0}){ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }
# Two values in the phone number
> db.foo.find({'user.phone':{'$size':2}},{'_id':0}){ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
11. exists determines whether the existence exists, type determines the type, and Sort sorts.
# The Value of "name" indicates the balanced type, and the value of "age" indicates the integer type. The value is in ascending order of name and age.
> db.foo.find({'name':{'$type':2},'age':{'$type':16}},{'_id':0}).sort({'name':1,'age':-1}){ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
# True if the value of name exists. # false if the value of name does not exist.
> db.foo.find({'name':{'$exists':true}},{'_id':0}){ "name" : "yiwa", "age" : 25, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.find({'name':{'$exists':false}},{'_id':0})>
12. $ elemMatch array element match
# Insert Test Data
> db.foo.save({x:[{'a':1,'b':5},999,'liw',{'a':12},{'b':100}]})WriteResult({ "nInserted" : 1 })
# Query the elements a = 1 and B = 5 in an element
> db.foo.find({'x':{'$elemMatch':{'a':1,b:{'$gt':4}}}},{'_id':0}){ "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }> db.foo.find({'x.a':1,'x.b':5},{'_id':0}){ "x" : [ { "a" : 1, "b" : 5 }, 999, "liw", { "a" : 12 }, { "b" : 100 } ] }
Update operation 1. update (criteria, objNew, upsert, multi), save () method
Criteria: Query condition for update, similar to
ObjNew: The update object and some updated operators (such as $, $ inc...) can also be understood as
Upsert: this parameter indicates whether to insert objNew if no update record exists. true indicates insertion. The default value is false.
Multi: the default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all the records identified by the condition are updated.
The save () method is equivalent to when both upsert and multi are true.
> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.update({'age':{$gte:30}},{$set:{'age':55}},fasle,false)2014-05-31T19:36:05.407+0800 ReferenceError: fasle is not defined> db.foo.update({'age':{$gte:30}},{$set:{'age':55}},false,false)WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 55, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.update({'age':{$gte:30}},{$set:{'age':56}},false,false)WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.update({'age':{$gte:300}},{$set:{'age':56}},true,false)WriteResult({"nMatched" : 0,"nUpserted" : 1,"nModified" : 0,"_id" : ObjectId("5389bee8afce65313a5614fa")})> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 75, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 85, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }{ "age" : 56 }> db.foo.update({'age':{$gte:30}},{$set:{'age':56}},true,true)WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 2 })> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 56, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }{ "age" : 56 }
2. $ inc adds value to the numeric field value
# All age values greater than 30 increase by 20
> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 58, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.update({'age':{$gte:30}},{$inc:{'age':20}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
3. $ set is equivalent to SQL's set field = value
# For age = 56, set it to name = 'laoda' and age = 65
> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "erwa", "age" : 56, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }> db.foo.update({'age':56},{$set:{'name':'laoda','age':65}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }
# Only one data record is updated, because the default value of multi is false.
4. $ unset delete a field
# Query the data with name = 'laoda' and delete the age field with age = 65 from the data with the user Field
> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "laoda", "age" : 65, "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }{ "age" : 65, "name" : "laoda" }> db.foo.update({'name':'laoda','user':{$exists:true}},{$unset:{"age":65}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.foo.find({},{'_id':0}){ "name" : "yiwa", "age" : 78, "user" : { "phone" : [ 123, 13, 186 ] } }{ "name" : "laoda", "user" : { "phone" : [ 63, 188, 13, 186 ] } }{ "name" : "sanwa", "age" : 56, "user" : { "phone" : [ 186 ] } }{ "name" : "siwa", "age" : 15, "user" : { "phone" : [ 63, 137 ] } }{ "age" : 65, "name" : "laoda" }
5. $ push operations under the Array
# Append the value to the field. The field must be of the array type. If the field does not exist, a new array type will be added.
> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda" }> db.array.update({'name':'laoda','age':65},{$push:{"phone":65}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 65 ] }> db.array.update({'name':'laoda','age':65},{$push:{"phone":[65,75,{'iphone':'188'},85]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 65, [ 65, 75, { "iphone" : "188" }, 85 ] ] }
6. $ operation under pushAll Array
# You can append multiple values to an array at a time.
> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda" }> db.array.update({'name':'laoda','age':65},{$pushAll:{"phone":[111,222]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222 ] }> db.array.update({'name':'laoda','age':65},{$pushAll:{"phone":[111,222]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222 ] }
7. $ addToSet Array Operation
# Add a value to the array, and add it only when the value is not in the array # insert it twice. If this value exists, it is not inserted.
> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }> db.array.update({'name':'laoda','age':65},{$addToSet:{"phone":333}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }> db.array.update({'name':'laoda','age':65},{$addToSet:{"phone":333}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }
8. $ pop delete a value in the array
# Delete the last value: {$ pop: {field: 1} Delete the first value: {$ pop: {field:-1 }}
Note: Only one value can be deleted, that is, only 1 or-1 can be used, but 2 or-2 can be used to delete two
> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ], 333 ] }> db.array.update({'name':'laoda','age':65},{$pop:{"phone":1}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 222, 111, 222, [ 111, 222 ] ] }> db.array.update({'name':'laoda','age':65},{$pop:{"phone":-1}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222, [ 111, 222 ] ] }> db.array.update({'name':'laoda','age':65},{$pop:{"phone":2}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 222, 111, 222 ] }> db.array.update({'name':'laoda','age':65},{$pop:{"phone":333}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 222, 111 ] }> db.array.update({'name':'laoda','age':65},{$pop:{"phone":-333}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111 ] }
# Test shows that as long as it is a positive integer, the negative number is deleted from the header.
9. $ delete a value equal to the value in the pull array field
> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 333 ] }> db.array.update({'name':'laoda','age':65},{$pull:{"phone":333}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111 ] }
10. $ remove multiple values from the pullAll array field
> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 111, 333, 222 ] }> db.array.update({'name':'laoda','age':65},{$pullAll:{"phone":[111,222]}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.array.find({},{'_id':0}){ "age" : 65, "name" : "laoda", "phone" : [ 333 ] }