Common commands of MongoDB (CRUD)

1, Database operation

1) Select and create database syntax:

use database name

The rule of the use command is to switch if the corresponding database exists, and create if it does not exist

2) Deletion of database

db.dropDatabase()

3) View databases with permissions

show dbs or show databases

Note: when you use the use command to create a new data, you can't see the data you just used with show dbs, because show dbs looks at the database on the disk, and the database you created is still in memory. It will be persisted to the disk only when the newly created database has a collection;
4) View the database currently in use

db

Simple operation of connecting to local MongoDB database through shell

>*******************Create a new library and switch to the new library. The existing library will be switched directly*******************
> use mydb
switched to db mydb
>
>******************Displays the libraries currently in use********************
> db
mydb
>
>*******************Delete current library*******************
> db.dropDatabase()
{ "ok" : 1 }
>
>*******************Show all libraries*******************
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>

2, Collection operation

A set is a table that corresponds to relational data

1) Display creation of collection (display creation)

DB. Createconnection ("collection name")

2) Collection view

show collections

3) Collection deletion

db. Collection name. drop() or db.collection.drop()

give an example:

>******************View the library currently in use*******************
> db
mydb
>
>*******************Create collection( mysql (table of)*******************
> db.createCollection("myCollection")
{ "ok" : 1 }
>
>*******************Query all collections under the current library*******************
> show collections
myCollection
>
>*******************Delete collection*******************
> db.myCollection.drop()
true

4) Collection implicit creation is to create a document. There is no corresponding collection, and the system creates it automatically

3, Basic CRUD of the document

Documents are relational data, one by one, but the document data structure of mongodb is basically consistent with JSON;
1) , document insertion

Single document insertion:

Db.mycollection.insert (data to insert) or db.mycollection.save (data to insert)

Batch document insertion:

db.myCollection.insertMany([data to insert, data to insert, data to insert...])

Note: if an exception occurs during batch insertion, the subsequent data will not be executed, and the previously inserted data will not be rolled back;

try{}catch {} to output exceptions

try {insert statement} catch (E) {print (E);}

Document query:

db. Collection name. find() or db. Collection name. Find ({condition you want to query})

give an example:

>*******************Insert a piece of data*******************
> db.myCollection.insert({"_id":"1","name":"Zhang San","age":NumberInt(10),"time":new Date()})
WriteResult({ "nInserted" : 1 })
>
> db.myCollection.find()
{ "_id" : "1", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:51:59.724Z") }
>
>******************Batch insert multiple pieces of data*******************
> db.myCollection.insertMany([{"_id":"2","name":"Zhang San","age":NumberInt(10),"time":new Date()},{"_id":"3","name":"Zhang San","age":NumberInt(10),"time":new Date()}])
{ "acknowledged" : true, "insertedIds" : [ "2", "3" ] }
>
> db.myCollection.find()
{ "_id" : "1", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:51:59.724Z") }
{ "_id" : "2", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:53:53.746Z") }
{ "_id" : "3", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:53:53.746Z") }
>

2) , query of documents
Query all data of a collection:

db. Collection name (. Find)

Query by criteria:

db. Collection name. find({criteria you want to query})

Query a limit 1 similar to mysql according to the criteria:

db. Collection name. findOne({criteria you want to query}) or db. Collection name. findOne()

Projection query, query the parameters you want to display

db. Collection name. findOne({condition you want to query}, {field to display: 1, do not want to display id: 0})

> db.myCollection.find()
{ "_id" : "1", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:51:59.724Z") }
{ "_id" : "2", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:53:53.746Z") }
{ "_id" : "3", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T11:53:53.746Z") }
{ "_id" : "5", "name" : "Zhang San", "age" : 10, "time" : ISODate("2021-12-04T12:12:30.737Z") }
{ "_id" : "6", "name" : "Li Si", "age" : 10, "time" : ISODate("2021-12-04T12:12:30.737Z") }
>
>*******************Check the data named Li Si*******************
> db.myCollection.find({"name":"Li Si"})
{ "_id" : "6", "name" : "Li Si", "age" : 10, "time" : ISODate("2021-12-04T12:12:30.737Z") }
>
>*******************Check the data named Li Si, but only show it name field id Fields are displayed by default*******************
> db.myCollection.find({"name":"Li Si"},{"name":1}))
{ "_id" : "6", "name" : "Li Si" }
>*******************Check the data whose name is Li Si, and only show the name, id Nor show*******************
> db.myCollection.find({"name":"Li Si"},{"name":1,"_id":0})
{ "name" : "Li Si" }
>

For more complex query operations, see section 5) 6) below;

3) , document update

Overwrite document updates

db. Collection name. update({update condition}, {updated content})

Note: This update will replace all the previous fields, leaving the content you updated

Local modification

db. Collection name. update({update condition}, {$set: {updated content})

Note: if multiple pieces of data are matched, such modification will only modify the first piece of data

Batch modification

db. Collection name. update({update condition}, {$set: {updated content}, {multi:true})

Column value increment

db. Collection name. update({update condition}, {$inc: {updated content}})

give an example:

> db.myCollection.find()
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "2", "name" : "Li Si", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
>
>*******************Overwrite modification id The data with 1 will be replaced with{"age":27}*******************
> db.myCollection.update({_id:"1"},{"age":NumberInt(27)}))
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.myCollection.find()
{ "_id" : "1", "age" : 27 }
{ "_id" : "2", "name" : "Li Si", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
>
>*******************modify id Equal to 2, the age of this piece of data is 33 (if multiple pieces are matched, only the first one will be modified)*******************
> db.myCollection.update({"_id":"2"},{$set:{"age":NumberInt(33)}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.myCollection.find()
{ "_id" : "1", "age" : 27 }
{ "_id" : "2", "name" : "Li Si", "age" : 33, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
>
>*******************The name of the data with the modified age of 10 is Caesar the great (batch modification)*******************
> db.myCollection.update({"age":10},{$set:{"name":"Caesar the great"}},{multi:true})))
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
>
> db.myCollection.find()
{ "_id" : "1", "age" : 27 }
{ "_id" : "2", "name" : "Li Si", "age" : 33, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
>
>*******************to id Add 2 to the age of this data*******************
> db.myCollection.update({"_id":"1"},{$inc:{"age":NumberInt(2)}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.myCollection.find()
{ "_id" : "1", "age" : 29 }
{ "_id" : "2", "name" : "Li Si", "age" : 33, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
>

4) , document deletion
Delete according to condition

db. Collection name. Remove (condition) or db. Collection name. Deletemany (condition)

Delete all data under the collection

db. Collection name. remove({})

give an example

> db.myCollection.find()
{ "_id" : "2", "name" : "Li Si", "age" : 33, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "1", "age" : 29 }
{ "_id" : "5", "age" : 29 }
>
>*******************Delete data aged 29*******************
> db.myCollection.remove({"age":29})
WriteResult({ "nRemoved" : 2 })
>
> db.myCollection.find()
{ "_id" : "2", "name" : "Li Si", "age" : 33, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "3", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
{ "_id" : "4", "name" : "Caesar the great", "age" : 10, "time" : ISODate("2021-12-05T04:18:54.868Z") }
>
>*******************Deletes all data in the specified collection*******************
> db.myCollection.remove({})
WriteResult({ "nRemoved" : 3 })
>
>
>db.myCollection.find()
>

5) Paging query of documents

Statistical query

db. Collection name. Count (no condition counts all)

Returns the specified number of records

db. Collection name. Find(). Limit (numeric)

Skip the specified data to get the specified data

db. Collection name. Find(). Skip (numeric)

Paging query is a combination of limit() and skip()

db. Collection name. Find(). Limit (page capacity). Skip (start of query = (current page - 1) * page capacity)

Sort query

db. Set name. Find(). Sort (condition: - 1)- 1 is in descending order and 1 is in ascending order. Multiple conditional sorting can be supported at the same time

give an example:

>
> db.myCollection.find()
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "2", "name" : "Li Si", "age" : 43, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "5", "name" : "pseudo-ginseng", "age" : 9, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>*******************Counts the total amount of all data in the specified collection*******************
> db.myCollection.count()
5
>*******************Statistics age equal to 10*******************
> db.myCollection.count({"age":10})
2
>*******************Skip the first three pieces of data*******************
> db.myCollection.find().skip(3)
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "5", "name" : "pseudo-ginseng", "age" : 9, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>*******************Paging query*******************
> db.myCollection.find().limit(2).skip(0)
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "2", "name" : "Li Si", "age" : 43, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
> db.myCollection.find().limit(2).skip(2)
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
> db.myCollection.find().limit(2).skip(4)
{ "_id" : "5", "name" : "pseudo-ginseng", "age" : 9, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>******************In descending order of age, then id Ascending data*******************
> db.myCollection.find().sort({age:-1},{_id:1})
{ "_id" : "2", "name" : "Li Si", "age" : 43, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "5", "name" : "pseudo-ginseng", "age" : 9, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>

6) Complex query
mongo supports regular queries

Fuzzy query

db. Collection name. find({name: / piece /})

Queries data that begins with the specified field

db.myCollection.find({name: / ^ Wang /}))

Range query

<, < =, >, > = this operator is also very common. The format is as follows:

db. Set name. find ({"field" {$gt: value}}) / / greater than: field > value
db. Set name. find ({"field" {$lt: value}}) / / less than: field < value
db. Set name. find ({"field" {$gte: value}}) / / greater than or equal to: field > = value
db. Collection name. find ({"field" {$lte: value}}) / / less than or equal to: field < = value
db. Collection name. find({"field" ({$ne: value}}) / / not equal to: field! = value

Example: query the records with the payment amount of comment points greater than 700

db . comment.find({likenum:{$gt:NumberInt(700)}})

Include or exclude queries

db. The collection name. find({age:{$in:[23,9]}}) contains data aged 23 and 9

db. The collection name. find({age:{$nin:[23,9]}}) does not contain data aged 23 and 9

Conditional query and or

give an example:

> db.myCollection.find()
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "2", "name" : "Li Si", "age" : 43, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "5", "name" : "pseudo-ginseng", "age" : 9, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>
>*******************Fuzzy query of data with name and sheet*******************
> db.myCollection.find({name:/Zhang/})
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
> db.myCollection.find({name:/^king/}))
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>*******************Query data older than 10*******************
> db.myCollection.find({"age":{$gt:10}})
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "2", "name" : "Li Si", "age" : 43, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>*******************Query age does not include 23,9 Data*******************
> db.myCollection.find({age:{$nin:[23,9]}})
{ "_id" : "2", "name" : "Li Si", "age" : 43, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>*******************Query age contains 23,9 Data*******************
> db.myCollection.find({age:{$in:[23,9]}})
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "5", "name" : "pseudo-ginseng", "age" : 9, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>
>
>*******************Query data older than 9 and younger than 40*******************
> db.myCollection.find({$and:[{"age":{$gt:9}},{"age":{$lt:40}}]})
{ "_id" : "1", "name" : "Zhang San", "age" : 23, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "3", "name" : "Wang Wu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
{ "_id" : "4", "name" : "Zhao Liu", "age" : 10, "time" : ISODate("2021-12-05T05:10:29.313Z") }
>

Summary:

Tags: Database MongoDB nosql

Posted on Sun, 05 Dec 2021 02:17:05 -0500 by SwiftlyTilting