MongoDB Common commands:
Super User Related:
Use admin
#增加或修改用户密码
Db.adduser (Ixigua, ' pwd ')
#查看用户列表
Db.system.users.find ()
#用户认证
Db.auth (Ixigua, ' pwd ')
#删除用户
Db.removeuser (' MongoDB ')
#查看所有用户
Show Users
#查看所有数据库
Show DBS
#查看所有的collection
Show collections
#查看各collection的状态
Db.printcollectionstats ()
#查看主从复制状态
Db.printreplicationinfo ()
#修复数据库
Db.repairdatabase ()
#设置记录profiling, 0=off 1=slow 2=all
Db.setprofilinglevel (1)
#查看profiling
Show profile
#拷贝数据库
Db.copydatabase (' mail_addr ', ' mail_addr_tmp ')
#删除collection
Db.mail_addr.drop ()
#删除当前的数据库
Db.dropdatabase ()
Client connections:
/usr/local/mongodb/bin/mongo 8.8.88/ixigualib-u ixigua-p ' pwd '
additions and deletions change:
#存储嵌套的对象
Db.foo.save ({' name ': ' Ysz ', ' address ': {' city ': ' Beijing ', ' Post ': 100096}, ' phone ': [138,139]})
#存储数组对象
Db.user_addr.save ({' Uid ': ' yushunzhi@sohu.com ', ' Al ': [' test-1@sohu.com ', ' test-2@sohu.com ']})
#根据query条件修改, insert if not present, allow multiple records to be modified
Db.foo.update ({' yy ': 5},{' $set ': {' xx ': 2}},upsert=true,multi=true)
#删除yy =5 Records.
Db.foo.remove ({' yy ': 5})
#删除所有的记录
Db.foo.remove ()
Index:
#增加索引: 1 (Ascending), -1 (Descending)
Db.things.ensureIndex ({firstname:1, lastname:1}, {unique:true});
#索引子对象
Db.user_addr.ensureIndex ({' Al.em ': 1})
#查看索引信息
Db.deliver_status.getIndexes ()
Db.deliver_status.getIndexKeys ()
#根据索引名删除索引
Db.user_addr.dropIndex (' al.em_1 ')
query:
#查找所有
Db.foo.find ()
#查找一条记录
Db.foo.findOne ()
#根据条件检索10条记录
Db.foo.find ({' msg ': ' Hello 1 '}). Limit (10)
#sort排序
Db.deliver_status.find ({' From ': ' ixigua@sina.com '}). Sort ({' Dt ',-1})
Db.deliver_status.find (). Sort ({' Ct ': -1}). Limit (1)
#count操作
Db.user_addr.count ()
#distinct操作
Db.foo.distinct (' msg ')
#> operation
Db.foo.find ({"timestamp": {"$gte": 2}})
#子对象的查找
Db.foo.find ({' address.city ': ' Beijing '})
Management:
#查看collection数据的大小
Db.deliver_status.dataSize ()
#查看colleciont状态
Db.deliver_status.stats ()
#查询所有索引的大小
Db.deliver_status.totalIndexSize ()
#查看当前所使用的数据库
Db
Author Mahout
This is supplemented by other netizens:
Library operations
show dbs
View the list of databases in the system
Note: Content must be present in the database before it is displayed
db = Db.getsiblingdb ("< database name >")
Or
Use < database name >
Toggle Current Database
Note: If the database does not exist, create the database
Either method is acceptable, and the DB value is set to the specified database, and you can then use DB to manage the new current database.
Db.dropdatabase () deletes the current database
Note: Delete the current database and then create a collection using the handle without changing the current database, and the deleted database will be recreated.
Db.copydatabase ("< current database name >", "< Backup library name >", [hostname])
Copy the database and create a database that is identical to the name
Note: The optional hostname parameter specifies the current library MONGDB server host name.
DB Displays the current database
Collection operations
Show Collections View a list of the collections contained in the current database
Db.createcollection ("< collection name >", {<options>}) Create Collection
Note: Parameter two optional objects
Attribute description
Capped Boolean, if true, indicates that the collection is a cap collection and does not grow to be larger than the size attribute designation. Default: False
Autoindexid Boolean, if True, indicates that a _id field is automatically created for each document added to the collection and the index is implemented on that field. This set of caps should be false. Default True
Size byte units, used to cap the collection. The oldest files are deleted to make room for new files
Max number of documents allowed in the CAP collection. The oldest files are deleted to make room for new files
db.< collection name >.drop ()
Or
coll = db.getcollection ("< collection name >")
Coll.drop () Delete collection
db.< collection name >.find ()
Or
coll = db.getcollection ("< collection name >")
Coll.find (query) View the document in the collection
Note: The optional query parameter specifies that the query document that contains the fields and values matches the document in the collection and returns the matching content. Example: Coll.find ({speed: "120mph"})
db.< Collection name >.insert ({})
Or
db.< Collection name >.save ({})
Inserts document data into the collection (if the collection does not exist, it is created, save also)
Treat the same kind of data as a collection
Properties in a collection that are specific to the data
A collection is a container in which more than n data is placed inside
These data can be filtered, sorted
db.< Collection Name >.remove ({filter Criteria},false)
Remove document parameter 1 from the collection: Filter criteria. Parameter 2: Optional parameter, whether single row delete, default false.
db.< Collection name >.save ({"_id": ObjectId ("57e26b294a655f35e13d6f5d"), "name": "Hung", "Age": 18})
Or
db.< Collection Name >.update ({filter Criteria}, {$set: {"name": "Jin", "Up": true}},
{upsert:true,multi:true}) Updating documents in a collection
Parameter one: conditions for updating documents
Parameter two: Update operators used when updating, and update properties
Common operator: $inc recursively the field value. $set set the field value. $push pushes an entry to an array. $rename Rename the field. And so on ·
Parameter three: optional, two properties. multi (all document updates to match, default false to update only the first) and Upsert (if no match is found, create one, default false is not created), Boolean.
*******************************************************************
db.< collection name >.pretty () neat display of query documents
db.< collection name >.find (). Sort ({properties: 1/-1}) sorted by specified property
1 is the positive sequence,-1: Reverse
db.< collection name >.find (). Limit (n) displays top N data
db.< collection name >.find (). Skip (n) skip top N Data
Condition Lookup
db.< Collection Name >.find ({property: value}) Property = = value
db.< Collection Name >.find ({property: {operator: value}})
Operator:
$lt less than
$lte less than or equal to
$GT Greater than
$gte is greater than or equal to
$ne is not equal to
Use two operators for the same property
db.< Collection Name >.find ({property: {operator 1:value, operator 2:value}})
such as:db.< collection name >.find ({age: {$gt: 6, $lt: 16}})
Define conditions for different properties at the same time (two conditions are set up simultaneously)
db.< Collection Name >.find ({Property 1: {operator: value}, property 2: {operator: value}})
or conditions
db.< Collection name >.find ({
$or: [
{property: Value},
{property: Value},
{Property: Value}
]
})
Using functions to filter data
db.< Collection name >.find ({
$where: function () {
Return True/false
}
})