Basic operation of mongo --- addition, deletion, modification and query of documents

4、CRUD

The mongo database is the same as the usual relational database. The most basic operation is to add, delete, modify and check. The only difference is that the name is different.

SQL terms/concepts MongoDB terms/concepts explain
database database database
table collection Database Table/Collection
row document Data record line/document
column field data fields/fields
index index index
table joins Table join, MongoDB does not support
primary key primary key Primary key, MongoDB automatically sets the _id field as the primary key

4.1 Insert document

The essence of inserting a document is to insert data into the collection, which is explained in terms of a relational database向数据表中插入数据

语法

#single insert

db.collection_Name.insertOne(document)

#multiple inserts

db.collection_Name.insertMany([document])

example:

#单个插入
db.test1.insertOne({name:"mongo",type:1,value:1})
#多个插入
db.test1.insertMany([
       {
         name:"mysql",
         type:2,
         value:2
       },{
         name:"oracle",
         type:3,
         value:3
       },{
         name:"nosql",
         type:4,
         value:4
       },{
         name:"xiaoming",
         sex:"男",
         age:20,
         address:"江苏省南京市浦口区××××××苑×××××单元×××××楼×××××××户"
       }
       ]
)

# 查看集合中的文档
db.test1.find()

insert image description here

4.2 Update Documentation

The essence of updating a document is to update the data in the document, which is explained in terms of a relational database更新数据表中的数据

```Syntax:`

#Single line update

db.test1.updateOne(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

#Multiple row update

db.test1.updateMany(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Parameter Description

  • query : query condition of update, similar to where in sql update query.
  • update : The updated content can also be understood as the set behind the sql update query
  • upsert : optional, the default is false, that is, do not insert when the update condition does not exist, true for insert, that is, insert data when the update condition does not exist.
  • multi : optional, the default is false, only the first record found will be updated, if this parameter is true, all multiple records found according to the condition will be updated.
  • writeConcern : optional, the level of exception thrown.

example:

  1. Update the first record without inserting

    db.test1.updateOne({name:"mongo"},{$set:{
         
         type:99}})
    

insert image description here

  1. Do not insert but update all documents matching the criteria
db.test1.updateMany({name:"redis"},{$set:{
   
   type:111}})

insert image description here

4.3 Query documents

The essence of querying documents is to query the data in the documents, which can be interpreted in terms of relational databases.查询数据表中的数据

语法

db.collection_name.find()

4.3.1 Common query

General query: search according to a specific condition

example:

#查找集合中全部的文档
db.test1.find()

insert image description here

#查找名字为mongo的文档
db.test1.find({name:"mongo"})

insert image description here

#查找值为空的文档
db.test1.find({
   
   value:null})

insert image description here

4.3.2 Fuzzy query

When operating data in the database, we often use fuzzy lookup, and fuzzy lookup is also supported in mongo, which needs to be used$regex

example

#查找名字中包含m的文档
db.test1.find({name:{$regex:/m/}})

insert image description here

#查找名字以m开头的文档
db.test1.find({name:{$regex:/^m/}})

insert image description here

4.3.3 Condition query

When operating data in the database, we often use conditional search, and conditional search is also supported in mongo
insert image description here

example

#查询类型不为111的文档
db.test1.find({type:{$ne:111}})

insert image description here

#查询类型不为111且名字已m开头的文档
db.test1.find({
   
   type:{$ne:111},name:{$regex:/^m/}})

insert image description here

#插叙名字为mongo或者type为空的文档
db.test1.find({
  $or:
    [
      {name:"mongo"},
      {
   
   type:null}
    ]
})

insert image description here

#插叙名字为mongo或者type为空且value为1的文档
db.test1.find({
  $or:
    [
      {name:"mongo"},
      {
   
   type:null}
    ],
    value:1
})

insert image description here

4.4.4 Projection query

The projection in MongoDB is to query the specified fields instead of directly querying all the fields of the document. 1 sign shows, 0 sign hides

语法

db.ollection_name.find({
   
   file:1,file2:0})

example

#插叙名字为mongo或者type为空且value为1的文档,要求只展示名字和类型
db.test1.find({
  $or:
    [
      {name:"mongo"},
      {
   
   type:null}
    ],
    value:1
},{_id:0,name:1,type:1})

insert image description here

4.4.5 Limit query

Limit query in mongo is to limit the amount of data queried

语法

db.test1.find().limit(count)

Parameter Description

count: the number of items to be limited

example:

#限制输出2条
db.test1.find().limit(2)

insert image description here

4.4.6 Skip query

Jump query in mongo refers to skipping the specified number of items, and then finding documents that meet the conditions

语法

db.test1.find().skip(count)

Parameter Description

count: the number of skipped items

example

#跳过前两条
db.test1.find().skip(2)

insert image description here

#跳过前两条查找,并且返回3条数据,
db.test1.find().skip(2).limit(3)
#跳过前两条查找,并且返回3条数据
db.test1.find().limit(3).skip(2)

In aggregation query, limit and skip have a sequence relationship

insert image description here

4.4.7 Sort queries

Sorting query refers to sorting the results of the query, where 1 is for ascending order and -1 is for descending order.

语法

db.COLLECTION_NAME.find().sort({
   
   KEY:1})

example

db.test1.find().sort({name:-1})

insert image description here

4.4. Deleting documents

Deleting a document in mongo refers to deleting the data in the collection

语法

single delete

db.collection.deleteOne(filter, options)

multiple delete

db.collection.deleteMany(filter, options)

Parameter Description

  1. The filter parameter is used to specify the document conditions that need to be deleted
  2. The options parameter can be used to specify some additional options

example

db.test1.deleteOne({
   
   type:null})
db.test1.deleteMany({
   
   type:null})

Guess you like

Origin blog.csdn.net/qq_40520912/article/details/130690196