table of Contents
Shell interface enter 0.
# 进入第一篇我们部署的mongoDB单机版目录
cd /opt/mongodb/bin
./mongo localhost:27017
1. database operations
1.1 View database
show dbs;
1.2 Creating a database
use xswl;
Please note that after the completion of the implementation of this sentence, and then perform the show dbs;
you will find xswl actually not in the list, it is because in MongoDB, the collection is created only when content is inserted! That is, create a collection (data table) again after the insertion of a document (record), the collection will really create.
so you need to perform insert operation, then -> show dbs; to see the.
1.3 Delete Database
db.dropDatabase();
2. set operations
2.1 increase collections
Equivalent to a set of database tables, but you can not use statements mongodb created, 'specified set directly into the table automatically created.
# 无参数
db.createCollection("you collection Name");
# 有参数
db.createCollection("colName", options);
Specific options to see: https://www.runoob.com/mongodb/mongodb-create-collection.html
2.2 Delete Collection
db.${collectionName}.drop();
2.3 View collection
show tables;
# 或者
show collections;
3. Data Operation
3.1 Insert Data
db.COLLECTION_NAME.insert(document);
db.col.insert({title: 'MongoDB 教程',
description: 'MongoDB 是一个 Nosql 数据库',
by: '菜鸟教程',
url: 'http://www.runoob.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
3.2 update data
# 基本语法: db.collection.update({query}, {$set:{key:value}});
db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})
The details look at: https://www.runoob.com/mongodb/mongodb-update.html
3.3 to delete data
db.col.remove({query});
# 删除全部数据
dn.col.remove({});
3.4 query data
db.col.find({and}, {projection});
https://www.runoob.com/mongodb/mongodb-query.html
3.5 conditions operation marks
- $gt --> greater than --> >
- $gte --> gt equal --> >=
- $lt -->less than --> <
- $lte --> lt equal --> <=
- $ne --> not equal --> !=
- $eq --> equal --> =
# select * from col where likes > 100;
db.col.find({likes : {$gt : 100}})
3.6 limit & skip
# 通常用来做分页, 只会显示第二条. 跳过一条, 显示一条. 先skip再limit, 和顺序没有关系
db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
Supplement skip and limit method is only suitable for small amount of data paging, and if one million efficiency will be very low because the method is to skip a number of pieces of data in the past, it is recommended to use where_limit
After reviewing some of the data and we found that all the information is this to say:
Do not do a query using skip, or a large amount of data will lead to a sharp decline in performance, because the skip is one of a number over, more naturally slow.
So we should avoid the use of the skip, how to avoid it? First, let's review a page after SQL timestamp paging scheme, the orderly nature of this field of use, the way to get data using queries, you can avoid direct out .? that is a lot of counting, if such a condition that comes on the query efficiency will improve and in fact this is what we have to verify:
Here we assume that query data Article 100001, Amount value of this data is: 2399927, we have to write two statements are as follows:
b.test.sort ({ "AMOUNT":}. 1). Skip (100000) .limit (10) // 183ms
db.test.find ({AMOUNT: {$ gt: 2399927}}). Sort ({ "AMOUNT ":. 1}) limit ( 10) // 53ms
results have been shipped to the comments, it is clear that the latter's performance is one-third of the former, the gap is very big also confirmed the theory poor skip efficiency.
3.7 sort
# 1 为正序, -1 为倒序
db.COLLECTION_NAME.find().sort({KEY:1})
skip (), limilt (), sort () performs three put together when the order of execution is to Sort (), then the Skip (), and finally the limit shown in ().