MongoDB
is a document-oriented, scalable, high-performance open source database. Typical application scenarios include web page data, caching, and alternative document storage.
Memorizing and using commands is a basic skill. Here is a quick checklist, which can be used as a desk manual.
library operations | |
switch or use a database | use mymongodb |
---|---|
see all libraries | show dbs |
delete the currently used database | db.dropDatabase() |
Clone all libraries to the current link | db.cloneDatabase(“192.160.1.1”) |
Copy the specified library | db.cloneDatabase(“sourcedb”,”targetdb”,”192.168.1.1”) |
View current database | db.getName() |
Current database state | db.stats() |
Current database version | db.version() |
View current database connections | db.getMongo() |
User action | |
Add user | db.addUser(“user_name”, “password”, true) |
User Authentication | db.auth(“username”, “password”) |
show all users | show users; |
delete users | db.removeUser(“username”); |
Collection basic information | |
Query the number of data in the collection | db.myCollection.count(); |
View data space size | db.myCollection.dataSize(); |
View the database where the collection is located | db.myCollection.getDB(); |
The current state of the aggregation | db.myCollection.stats(); |
The total size of the current collection | db.myCollection.totalSize(); |
Collection storage size | db.myCollection.storageSize(); |
Shard version information | db.myCollection.getShardVersion(); |
Collection rename | db.myCollection.renameCollection(“targetCollection”); |
delete collection | db.myCollection.drop(); |
Addition, deletion and modification of collection data | |
add record | db.myCollection.save({mykey:”t_key”,myvalue:”t-value”}); |
删除记录 | db.myCollection.remove({mykey:”t_key”}); |
修改记录 | db.myCollection.update({condition: xx}, {$set: {field: ‘changefield’}}, false, true); |
查询并修改记录 | db.myCollection.findAndModify(query: {condition1: {gte: XX}}, sort: {condition2: -1}, update: {gte: XX}}, sort: {condition2: -1}, update: {set: {target1: 'yy'}, $inc: {target2: 2}}, remove: true}); |
集合数据查询 | |
查询所有记录 | db.myCollection.find(); |
查询第一条记录 | db.myCollection.findOne(); |
数据去重 | db.myCollection.distinct(“fieldname”); |
数值区间查询 | db.myCollection.find({numfield:{$gte:nn}}); |
字符串查询 | db.myCollection.find({targetfield:/ABC/}); |
指定字段查询 | db.myCollection.find({},{field1:’abc’,field2:nnn}); |
指定返回条数查询 | db.myCollection.find().limit(m).skip(n); |
排序 | db.myCollection.find().sort({targetfield:-1}); //降序 |
统计记录数 | db.myCollection.find({target: n }).count(); |
索引操作 | |
创建 | db.myCollection.ensureIndex({targetfield: 1}); |
查询所有索引 | db.myCollection.getIndexes(); |
查询所有索引大小 | db.myCollection.totalIndexSize(); |
查询索引信息 | db.myCollection.reIndex({targetfield: 1}); |
删除指定索引 | db.myCollection.dropIndex(“targetfield”); |
删除所有索引 | db.myCollection.dropIndexes(); |
辅助命令 | |
查询错误信息 | db.getPrevError(); |
清空错误信息 | db.resetError(); |
各种帮助信息 | help; db.help(); db.myCollection.help(); db.myCollection.find().help(); rs.help(); |