Basic commands

List all databases

show databases
show dbs
Copy the code

Accessing the database

Use Database nameCopy the code

Viewing the Current Database

db
Copy the code

Lists all collections in the current database

show collections
Copy the code

Empty the collection

db.<collection>.remove({})
Copy the code

Delete the collection

db.<collection>.drop()
Copy the code

Deleting a Database

db.dropDatabase()
Copy the code

Insert data

Inserts data into the collection

Db. < collection >. Insert ({" name ":" ha ha "king," age ": 24})Copy the code
Db. < collection >. Insert ([{" name ":" zhang ", "age" : 24}, {" name ":" bill ", "age" : 24}, {" name ":" detective ", "age" : 24}, {" name ": }])Copy the code
db.demo.insertOne({"name": "abc"})
Copy the code
db.demo.insertMany([
    {"name": "wang1", "age": 24},
    {"name": "wang2", "age": 23}
])
Copy the code

Query data

Query all data in the collection

db.<collection>.find()
Copy the code

Query a single piece of data in a collection

db.<collection>.find_one()
Copy the code

Find data by criteria

db.<collection>.find({"name": "wanghaha", "age": 24})
db.<collection>.findOne({"name": "wanghaha"})
db.<collection>.findOne({"name": "wanghaha"}).name
Copy the code

Get data length

db.<collection>.find().count()
db.<collection>.find().length()
Copy the code

Modify the data

Modify the data

Db.<collection>. Update (query condition, new object)Copy the code

Update () replaces old objects with new ones by default, and only one is modified by default

$set can be used to modify specified attributes in a document. $unset can be used to delete specified attributes in a document

Modify a document that meets the criteria

db.demo.update({"name": "hello"}, {
    $set: {"age": 23}
})
Copy the code
db.demo.updateOne(
    {"_id": ObjectId("5f34bcc44040779b0fc1c207")},
    {$set:{"name": "wanghaha"}}
)
Copy the code

Delete the document properties that match the criteria

The db. The demo. UpdateOne ({" name ":" horse six "}, {$unset: {" name ":" horse six "}})Copy the code

Modify multiple documents that meet the conditions

db.demo.update(
    {"name": "wanghaha"}, 
    {
        $set:{"sex": 2},

    },
    { 
        multi: true
    }
)
Copy the code
db.demo.updateMany({"name": "wanghaha"}, {$set:{"address": "china"}})
Copy the code

Replace the documents that meet the criteria

db.demo.replaceOne({"name": "wanghaha"}, {$set:{"age": 25}})
Copy the code

Delete the data

Delete a document

Db.<collection>.deleteone ({"name": "name"})Copy the code
db.<collection>.remove()
Copy the code

Delete one or more. Passing true as the second argument will delete only one. Passing an empty object will wipe out all data

Delete multiple documents that meet the requirements

db.<collection>.deleteMany()
Copy the code

Advanced operation

Adds the specified data to the document

Db.chart.update ({"_id":ObjectId("5f351285a4fc6a39518b5e84")}, {$push: {"legend. Data ":" old king "}})Copy the code

The specified data is appended to the array in the document, not if it already exists

Db.chart.update ({"_id":ObjectId("5f351285a4fc6a39518b5e84")}, {$addToSet: {"legend. Data ":" old king "}})Copy the code

Deletes one of the multilevel structured arrays

db.chart.update(
    {"_id":ObjectId("5f351285a4fc6a39518b5e84")},
    {$pull: {"legend.data":"wanghaha"}}
)
Copy the code

Deletes the last of a multilevel structured array

db.chart.update(
    {"_id":ObjectId("5f351285a4fc6a39518b5e84")},
    {$pop : { "legend.data" : 1 }}
)
Copy the code

Bulk insert

var arr = [];
for(var i=0; i<20000; i++){ arr.push({"num": i})
}

db.numbers.insert(arr)
Copy the code

The operator

Conditional operator

// query for values equal to 500
db.numbers.find({"num": 500})
db.numbers.find({"num": {$eq: 500}})
Copy the code
// Query values greater than 500
db.numbers.find({"num": {$gt: 500}})
Copy the code
// Query values greater than or equal to 500
db.numbers.find({"num": {$gte: 500}})
Copy the code
// Query values less than 500
db.numbers.find({"num": {$lt: 500}})
Copy the code
// Query values less than or equal to 500
db.numbers.find({"num": {$lte: 500}})
Copy the code
// query not equal to 500
db.numbers.find({"num": {$ne: 500}})
Copy the code
// The query is greater than 40 and less than 50
db.numbers.find({"num": {$gt:40.$lt:50}})
Copy the code
// Query values greater than 19990 or less than 10
db.numbers.find(
    {
        $or: [{"num": {$gt: 19990}}, {"num": {$lt: 10}}]})Copy the code
// Query all employees in finance department
var deptno = db.dept.findOne({name: 'Finance Department'}).deptno
db.emp.find({deptno: deptno})
Copy the code

Limit the length

Limit Sets the upper limit of the displayed data
db.numbers.find().limit(10)
Copy the code
Skip () is used to skip a specified amount of data
db.numbers.find().skip(10).limit(10)
Copy the code

Extension (paging logic)

/* Page logic displays 10 pages per page 1-10 11-20 21-30... * /Skip ((Current page number -1)* number of items per page). Limit (Number of items per page)Copy the code

Autoincrement and autodecrement operation

// Age increases by 5 for all
db.test.updateMany({}, {$inc: {age: 5}})
Copy the code
// Everyone's age decreases by 10
db.test.updateMany({}, {$inc: {age: -10}})
Copy the code

The sorting

ascending

// 1. Indicates ascending order
db.<collection>.find().sort({A ' 'field.1})
Copy the code

Descending order

// -1. Indicates descending order
db.<collection>.find().sort({'field B', -1})
Copy the code

Multifield sort

// Select A and B from each column in ascending order
db.<collection>.find().sort({A ' 'field: 1.'field B': -1})
Copy the code

projection

Only the specified fields are displayed

/ / projection
// Display only the specified field, 1 display, 0 do not display
db.<collection>.find({}, {field1: 1._id: 0})
Copy the code