MongoDB simple operation

Run in the bin directory, the db directory must have been created in advance, otherwise save it, and then you can use it

​​​​​mongod --dbpath d:\data\db

The default login does not require authentication. Can use compass for more intuitive use

create database

Use use to point to the database if it does not exist, but if no data is added, the database will still not be created

As you can see, the database will only be created after the operation is performed

remove use

db.dropDatabase()

 

Revise

By default update only modifies the first found data, and if set is not used, the entire document will be replaced

To modify multiple lines, you need to use the set and multi keywords, which match 10 records, but only 9 records have been modified due to the previous modification

 

save, if _id already exists, replace the original document, if not, add and set _id

db.lost.save({
    '_id': ObjectId('5a7faa972054091f9ceeeb01'),
    'name': 3333
})
db.lost.save({
    '_id': ObjectId('123456781234567812345678'),
    'name': 3333,
    'id':123412341234
})

Note that the _id length must be the same as the original, otherwise the save will fail

 

delete

delete all

delete only one

db.lost.remove({},{justOne:true})

 

comparison operation

Logical operations, separated by commas, indicate that a series of conditions must be met

 

Use or to indicate that one of the conditions in the array is satisfied.

 

set in, not range

 

Mixed, find results with id 111, 222 or id 333 and id not 111

 

The pit of range query will only find data smaller than 20

 

 

 

Custom query, not recommended, very slow

 

Regular matching, query the data starting with 2 in the specified range

 

limit skip completes paging, skip is the specified number of skips in the query results

 

Specifies the returned fields. _id is displayed by default, if it is not set, it means not displayed. If the field is set to 1, it means display.

 

 

sortsort

 

To count the number, use count, or omit find

 

deduplication

distinct('de-duplicated field', query condition)

 

Aggregation function, grouping documents, _id means, the field used for grouping, the number after sum means adding one each time there is a row

 

Count the sum of a field

 

The use of pipelines, first matching, then grouping, and pushing the data document into the specified field

 

Sort and specify display fields

 

View query plan

Build an index, 1/-1 indicates order or descending order

db.collection.ensureIndex({property, 1, -1})

Guess you like

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