Types of | Order | illustrate |
server level | db.serverStatus() | Server Status Information |
db.serverStatus().connections | Server connection information | |
db.fsyncLock() | Force refresh and add write lock | |
db.fsyncUnlock() | unlock | |
db.currentOp() | View the current lock status | |
help | help command | |
Database related commands | use dbname | select database |
show dbs | list all databases | |
db | show current database | |
db.dropDatabase() | delete the current database | |
db.stats() | View current library statistics | |
db.printCollectionStats() | View statistics for each collection | |
Security and Authentication | show users | list users |
db.addUser("username", "password") | Create ordinary users, can read and write | |
db.addUser("username", "password", true) | Create read-only user | |
db.removeUser("username") | delete users | |
db.auth("username", "password") | User Authentication | |
Collection operations | show collections | List the collections of the current database |
db.createCollection("c1") | Create collection c1 | |
db.c1.drop() | delete set c1 | |
db.c1.renameCollection("c2") | Rename set c1 to c2 | |
db.c1.count() | Displays the number of documents (rows) of collection c1 | |
db.createCollection("c3",{capped:true, size:10000}) | Create a special collection capped: true, indicating that the document structure of the collection cannot be modified; size: specify the space size of the collection, and the insertion operation will be APPENDed to the pre-allocated space file in order. The header starts overwriting the original data. Document deletion is not allowed, and updates cannot exceed the size of the original document. This kind of collection is very efficient, and it is suitable for some occasions where data is temporarily saved. |
|
index operation | db.c1.ensureIndex({name:1}) | Create a sequential index on the name field in collection c1 |
db.c1.ensureIndex({name:-1}) | Create an inverted index on the name field in collection c1 | |
db.c1.ensureIndex({name:1},{unique:1}) | Create a unique sequential index on the name field in collection c1 | |
db.c1.ensureIndex({name:1},{background:1}) | Create indexes in the background without blocking other operations | |
Inquire | db.c1.getIndexes() | View the index information of c1 |
db.c1.dropIndex("name_1") | drop the index named name_1 | |
db.c1.dropIndexes() | Drop all indexes except the _id primary key index | |
db.c1.totalIndexSize() | Display the total size of all indexes of the c1 collection | |
Additions, deletions and modifications | db.c1.insert({_id:1,name:"tom"}) | Insert a new document in collection c1 |
db.c1.update({_id:1},{name:"jack"}) | Modify the name field of the document whose _id is 1 | |
db.c1.remove({name:"jack"}) | Delete all documents with name: "jack" in collection c1 | |
db.c1.remove({name:"tom"},1) | Delete only the first document with name: "tom" in collection c1 | |
db.c1.save({_id:1,name:"lucy"}) | 新增或修改文档内容(主键已存在则修改,主键未指定或不存在则新增) | |
查询 | db.c1.find({_id:5}) | select * from c1 where _id=5 |
db.c1.find({_id:{$lt:5}}) | select * from c1 where _id<5 | |
db.c1.find({_id:{$lte:5}}) | select * from c1 where _id<=5 | |
db.c1.find({_id:{$gt:5}}) | select * from c1 where _id>5 | |
db.c1.find({_id:{$gte:5}}) | select * from c1 where _id>=5 | |
db.c1.find({_id:{$gt:0,$lt:9}}) | select * from c1 where _id>0 and id<9 | |
db.c1.find({_id:{$in:[1,3,5]}}) | select * from c1 where _id in (1,3,5) | |
db.c1.find({_id:{$nin:[1,3,5]}}) | select * from c1 where _id not in (1,3,5) | |
db.c1.find({_id:{$gt:9},name:"tom"}) | select * from c1 where _id>9 and name="tom" | |
db.c1.find({$or:[{_id:{$gt:3}},{name:"tom"}]}) | select * from c1 where _id>3 or name="tom" | |
db.c1.find({_id:{$gt:7}},{name:1,tel:1}) | select name,tel from c1 where _id>7 | |
db.cl.find().sort({name:1}) | select * from c1 order by name | |
db.cl.find().sort({name:1}) | select * from c1 order by name desc | |
db.cl.find().limit(10) | select * from c1 limit 10 | |
db.User.find({tel:{$exists:1}}); | 查询存在tel字段的文档 | |
db.User.find({name:/a/}); | select * from c1 where name like '%a%' | |
db.User.find({name:/^a/}); | select * from c1 where name like 'a%' |
MongoDB command summary
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326610600&siteId=291194637
Recommended
Ranking