MnongoDB installation and common commands

mongodb installation

We only talk about how to install in the ubuntu virtual machine here

1. Installation of mongoDB

  1. ubuntu
sudo apt-get install -y mongodb-org

The -y parameter here is the yes option in all installation processes, all of which default to yes, saving us the manual input operation.

  1. Other linux:
    tar -zxvf mongodb-linux-x86_64-ubuntu1604-3.4.0.tgz
  • Move to the /use=r/local/ directory
sudo mv -r mongodb-linux-x86_64-ubuntu1604-3.4.0/ /usr/local/mongodb
  • Add the executable to the PATH path
export PATH=/usr/local/mongodb/bin:$PATH

2. Start the server

View help:

mongod –help

start up:

sudo service mongod start

stop:

sudo service mongod stop

Reboot:

sudo service mongod restart

Check if the startup is successful:

ps ajx|grep mongod

The location of the configuration file:

    /etc/mongod.conf, 

Default port:

    27017

Location of logs:

/var/log/mongodb/mongod.log

3. Client

Start the local client:

mongo

View help:

mongo –help

quit:

exit
或者
ctrl+c

Fourth, the database common commands

  1. Basic commands about database:
  • View the current database:
        db
  • View all databases:
show dbs  /show databases
  • Switch database:
use db_name
  • Delete the current database:
db.dropDatabase()
  1. Basic commands for collections:

    • Without manually creating the collection:

    The collection is created the first time data is added to a non-existing collection

    • Create a bond manually:
db.createCollection(name,options)
db.createCollection("stu")
db.createCollection("sub", { capped : true, size : 10 } )
# 参数capped: 默认值为false表示不设置上限,值为true表示设置上限
# 参数size: 当capped值为true时, 需要指定此参数, 表示上限大小,当文档达到上限时, 会将之前的数据覆盖, 单位为字节
  • Check out the collection:
show collections
  • Delete a collection:
db.集合名称.drop()
  1. type of data
  • Object ID: Document ID

  • String: String, most commonly used, must be valid UTF-8

  • Boolean: stores a boolean value, true or false

  • Integer: Integer can be 32-bit or 64-bit, depending on the server

  • Double: Stores a floating point value

  • Arrays: Arrays or lists, storing multiple values ​​into one key

  • Object: for embedded documents, that is, a value is a document

  • Null: store Null value

  • Timestamp: Timestamp, indicating the total number of seconds from 1970-1-1 to the present

  • Date: UNIX time format to store the current date or time

    important point:

  • The create date statement is as follows: The format of the parameter is YYYY-MM-DD
  • new Date('2017-12-20')
  • Each document has an attribute, _id, to ensure the uniqueness of each document
  • You can set the _id to insert the document yourself. If it is not provided, then MongoDB provides a unique _id for each document, the type is objectID
  • objectID is a 12-byte hexadecimal number:
  • The first 4 bytes are the current timestamp
  • Next 3 bytes of machine ID
  • MongoDB's service process id in the next 2 bytes
  • The last 3 bytes are simple increment values
  1. insert
  • db.collection name.insert(document)
db.集合名称.insert({name:'tom',gender:1})
db.集合名称..insert({_id:"20180101",name:'tom',gender:1})
  • When inserting a document, if you do not specify the _id parameter, MongoDB will assign a unique ObjectId to the document
  • When inserting, if the values ​​of all parameters including the _id parameter exist, and the inserted document is the same as the existing one, an exception will be thrown.
  1. keep
db.集合名称.save(document)
  • Modify if the document's _id already exists, add if the document's _id does not exist

5. Query (*****)

  1. Simple query:
db.集合名称.find()
  1. renew:
db.集合名称.update(<query> ,<update>,{multi: <boolean>})
  • Parameter query: query condition
  • Parameter update: update operator
  • Parameter multi: optional, the default is false, which means that only the first record found is updated, and a value of true means that all documents that meet the conditions are updated. For example:
db.stu.update({name:'hr'},{name:'mnc'})   # 更新一条
db.stu.update({name:'hr'},{$set:{name:'hys'}})    # 更新一条
db.stu.update({},{$set:{gender:0}},{multi:true})   # 更新全部
  1. delete:
db.集合名称.remove(<query>,{justOne: <boolean>})
  • Parameter query: optional, the condition of the deleted document
  • Parameter justOne: optional, if set to true or 1, only one item will be deleted, the default is false, which means that multiple items will be deleted
  1. data query
  • find(): query
     db.集合名称.find({条件文档})
  • findOne(): query, only the first one is returned
    db.集合名称.findOne({条件文档})
  • pretty(): format the result
db.集合名称.find({条件文档}).pretty()
  1. comparison operator
  • Equal to: The default is equal to judgment, no operator
  • Less than: $lt (less than)
  • Less than or equal: $lte (less than equal)
  • Greater than: $gt (greater than)
  • Greater than or equal to: $gte
  • Not equal to: $ne chestnut:
db.stu.find({age:{$gte:18}})
  1. Logical Operators
  • and: write multiple conditions in json
  • Find people whose age is greater than or equal to 18 and whose gender is true
 db.stu.find({age:{$gte:18},gender:true})
  • or: use $or, the value is an array, and each element in the array is json
  • Query for people whose age is greater than 18, or whose gender is false
db.stu.find({$or:[{age:{$gt:18}},{gender:false}]})
  • Query age is greater than 18 or gender is male, and name is tom
db.stu.find({$or:[{age:{$gte:18}},{gender:true}],name:'tom'})
  1. Range operator:
  • Use "$in", "$nin" to determine whether it is within a certain range
  • Query for people aged 18, 28
db.stu.find({age:{$in:[18,28]}})
  1. Support regular expressions
  • Write regular expressions using // or $regex
  • Looking for people with the last name "Huang"
db.stu.find({name:/^黄/})
db.stu.find({name:{$regex:'^黄'}})
  1. limit and skip
  • limit(): used to read the specified number of documents
db.集合名称.find().limit(number)
  • Query 2 pieces of information
db.集合名称.find().limit(2)
  • skip(): used to skip a specified number of documents
db.集合名称.find().skip(number)
db.集合名称.find().skip(2)
  • Simultaneous use: In actual use, we often use them together.
db.集合名称.find().limit(4).skip(5)
db.集合名称.find().skip(5).limit(4)
  1. Custom query* (support js)
  • Write a function after $where to return the data that meets the conditions
  • Query for those older than 30
db.集合名称.find({
    $where:function() {
        return this.age>30;}
})
  1. projection
  • In the returned results of the query, only the necessary fields are selected
  • db.collection name.find({},{field name: 1,...})
  • The parameters are fields and values, a value of 1 means display, and a value of 0 does not display
  • Special: For the _id column, it is displayed by default. If it is not displayed, it needs to be explicitly set to 0
db.集合名称.find({},{_id:0,name:1,gender:1})
  1. sort sort(), for sorting
db.集合名称.find().sort({字段:1,...})
  • Parameter 1 is in ascending order
  • Parameter -1 is for descending order
  • Descending by gender, then ascending by age
db.集合名称.find().sort({gender:-1,age:1})
  1. Statistics
  • count(): count the number of documents in the result set
  • db.collection name.find({condition}).count()
    • In actual use, we generally use the first one which is more logical.
  • db.collection name.count({condition})
db.集合名称.find({gender:true}).count()
db.集合名称.count({age:{$gt:20},gender:true})
  1. Eliminate duplicates
  • distinct() deduplicates the data
  • db.collection name.distinct('de-duplication field',{condition})
db.集合名称.distinct('hometown',{age:{$gt:18}})

6. Aggregate query of query

Aggregate is an aggregation pipeline based on data processing. Each document passes through a pipeline consisting of multiple stages. The pipeline of each stage can be grouped, filtered and other functions, and then processed through a series of output. corresponding results.

db.集合名称.aggregate({管道:{表达式}})
  1. The commonly used pipeline is in mongodb. After the document is processed, the next processing is performed through the pipeline.
    Common pipelines are as follows:
  • $group: Group the documents in the collection, which can be used for statistical results
  • $match: filter data, only output documents that meet the conditions
  • $project: Modify the structure of the input document, such as renaming, adding, deleting fields, creating calculation results
  • $sort: sort the input documents and output them
  • $limit: Limit the number of documents returned by the aggregation pipeline
  • $skip: Skip the specified number of documents and return the rest
  • $unwind: split the fields of the array type
  1. Expressions process input documents and output
语法:表达式:'$列名'
##### 常用表达式:
  • $sum: Calculate the sum, $sum:1 means double count
  • $avg: Calculate the average
  • $min: get the minimum value
  • $max: get the maximum value
  • $push: Insert values ​​into an array in the resulting document
  • $first: Get the first document data according to the sorting of resource documents
  • $last: Get the last document data according to the sorting of resource documents
  1. Aggregate $group
  • Groups documents in a collection, which can be used for statistical results
  • _id represents a sentence of grouping, and the format of using a field is "$field"
  • Chestnut: Count the total number of men and women
db.集合名称.aggregate(
        {$group:
                {_id: "$gender", counter:{$sum:1}}
        }
)

group document link

  1. Group by null
  • Group all documents in a collection into a group
  • Chestnut: find the total number of people, the average age
db.集合名称.aggregate(
        {$group:
                {_id:null, counter:{$sum:1}, avgAge:{$avg:"$age"}}
        }
)
  1. pivot data
  • Count the names of people of different genders
db.集合名称.aggregate(
        {$group:{_id:"$gender", name:{$push:"$name"}}}
)
  1. match
  • Used to filter data and only enter documents that meet the conditions
  • Standard query operations using mongodb
  • Chestnut: Query people older than 20
db.集合名称.aggregate(
        {$match:{age:{$gt:20}}}
)
  • Chestnut 2: Query the number of men and women older than 20
db.集合名称.aggregate(
        {$match:{age:{$gt:20}}},{$group:{_id:"gender", counter:{$sum:1}}}
)
  1. $project
  • Modify the structure of the input document, such as: rename, add, delete fields, create calculation results
  • Chestnut: query name and age; only display the field data of the query, not the _id field
db.集合名称.aggregate(
        {$project:{_id:0, name:1, age:1}}
)
  • Chestnut 2: Query the number of men and women, only output the number of people, other do not output.
db.集合名称.aggtegate(
        {$group:{_id:"$gender"}, counter:{$sum:1}},
        {$projectL{_id:0, counter:1}}
)
  1. $sort
  • Sorting: Not much to explain. directly on the chestnut
  • Chestnut: Query the number of men and women, in descending order of the number of people
db.集合名称.aggregate(
        {$group:{_id:"$gender", counter:{$sum:1}}},
        {$sort:{counter:-1}}
)
  1. $limit与$skip
  • $limit: limit the number of documents returned by the aggregation pipeline
  • Chestnut: query 2 pieces of information
db.集合名称.aggregate({$limit:2})
  • $skip: Skip the specified number of documents and return the remaining document content
  • Chestnut: Query the information starting from the third item
db.集合名称。aggregate({$skip:2})
  • joint use
  • Count the number of men and women, in ascending order], take the second data
db.集合名称.aggregate(
        {$group:{_id:"$gender", counter:{$sum:1}}},
        {$sort:{counter:1}},
        {$skip:1},
        {$limit:1}
)

Here you need to pay attention, you must first jump: skip, then take: limit

Seven, MongoDB index creation

  • Index: to improve query speed

  • Test: Insert 100,000 pieces of data into the database

for(i=0;i<100000;i++){db.t12.insert({name:'test'+i,age:i})}

db.t1.find({name:'test10000'})
db.t1.find({name:'test10000'}).explain('executionStats')
  • After indexing, compare:

  • grammar:

db.集合.ensureIndex({属性:1}),1表示升序, -1表示降序
  • Specific operation:
    db.t1.ensureIndex({name:1})
db.t1.find({name:'test10000'}).explain('executionStats')

Compare the speed of two queries

Eight, data backup and recovery

Syntax for backup:

mongodump -h dbhost -d dbname -o dbdirectory
  • -h: server address, you can also specify the port number
  • -d: The name of the database to be backed up
  • -o: The backup data storage location, this directory stores the backed up data
  • Chestnut:
mongodump -h 192.168.196.128:27017 -d test1 -o ~/Desktop/test1bak

Recovery syntax:

mongorestore -h dbhost -d dbname --dir dbdirectory
  • -h: server address
  • -d: The database instance that needs to be restored
  • --dir: The location of the backup data
  • Chestnut:
mongorestore -h 192.168.196.128:27017 -d test2 --dir ~/Desktop/test1bak/test1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325062336&siteId=291194637