MongoDB Basic Query and monitoring

Source: Internet
Author: User
Tags mongodb

Basic view:

Show DBS; #查看所有的数据库
Show Collectios; #查看所有的集合
Use DB, switch database, create if not present
Db.dropdatabase (); #删除数据库

Additions and deletions to check:

Db.lhy.insert ({"Name": "Brigitte", "Age": "+", "Sex": "Nan"});
Db.zxy.insert ({"username": "lqx", "Password": "123456"});

Db.zxy.update ({"username": "zxy"},{"username": "Lhy", "Password": "123456"});
Db.zxy.insert ({"Name": "Lhy", "Age": "+", "Sex": "NV"});
Db.zxy.update ({"Age": "},{", "Age": 19});
Db.zxy.update ({"Age": "},{" $set: {' age ': 19}}); #如果要修改指定的key, just use $set.
Db.zxy.remove ({}); #删除整个集合里面的数据
Db.zxy.remove ({"Age": +, "name": "XXX"}); #删除指定的数据
Db.zxy.remove ({"Age": "18"});
Db.zxy.remove ({"Age": "},2"); #删除指定的条数
Db.zxy.find ({"Age": +, "name": "Sex"}); #查询指定的数据
Db.zxy.find (); #查看所有的;
Db.zxy.find ({"Name": "Lqx"}); #查看指定的数据;
Db.zxy.find ({"Name": "Lqx", "Age":); #and
Db.zxy.find (). Pretty (); #格式化数据
Db.zxy.find (). Count (); #查看行数
Db.zxy.find (). Sort ({"Name": -1}) #排序, 1 is ascending,-1 is descending
Db.zxy.find ({$or: [{"Sex": "Nan"},{"name": "Brigitte"}]}). Pretty (); #or操作
Db.zxy.find ({"Age": {$gte:}}). Pretty (); #做笔记操作, greater than or equal to 20
$gte: greater than or equal to
$GT: Greater Than
$lte: Less than or equal to
$LT: Less than

Profile (slow query):
Db.getprofilinglevel () #获取当然profile的级别
Db.setprofilinglevel (1,2000) #记录超过2s
Db.setprofilinglevel (2);
0: Do not open
1: Record slow command, default is greater than 100ms
2: Record all commands
Db.system.profie.find (); #查询慢查询的记录
TS: When the command is executed
OP: Type of operation
Query: Detailed information for this command
ResponseLength: Returns the size of the result set
Ntoreturn: The result set actually returned by this query
Millis: This command executes time-consuming, in milliseconds

Index:
Db.lhy.ensureIndex ({"Name": 1}) #创建一个普通索引
Db.lhy.ensureIndex ({"Name": 1, "Age": 1}); #多列索引
Db.lhy.ensureIndex ({"Name": 1},{"unique": true}); #唯一索引
Db.system.indexes.find (); #查看索引
Db.collections.getIndexes (); #查看当前集合中的索引
Db.lhy.dropIndex ({"Name": 1}); #删除指定索引
Db.user.dropIndexes (); Delete all indexes

Explain (parse):
Db.lhy.find ({"Name": "Zhaoxiaojing"}). Explain (); #解析查询语句
Millis: Time-consuming (ms
Indexbounds: The index used, if this field is not, the index is not used at all

MongoDB Basic Query and monitoring

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.