Learning goals
  1. Learn how to insert data into mongodb
  2. Master the method of storing data in mongodb
  3. Master the method of mongodb data query
  4. Know how to process mongodb query results
  5. Master the method of mongodb data update
  6. Learn how to delete data in mongodb

1. Insert data into mongodb

Db. Set name insert(document)

db.stu.insert({name:'gj', gender:1})
db.stu.insert({_id:"20170101", name:'gj', gender:1})
Copy the code

If the _id parameter is not specified, MongoDB automatically allocates a unique ObjectId for the document

2. Mongodb preservation

Db. Collection name. Save (document)

db.stu.save({_id:'20170101', name:'gj', gender:2})
db.stu.save({name:'gj', gender:2})
db.stu.find()
Copy the code

If the document _id already exists, change it; if the document _id does not exist, add it

3 mongodb query

Command: db.collection name.find ()

You can practice using the following data

db.stu.insert([{"name" : "Guo jing"."hometown" : "Mongolia"."age": 20."gender" : true },
{"name" : "Huang2 rong2"."hometown" : Peach Blossom Island."age": 18."gender" : false },
{"name" : "Hua zheng"."hometown" : "Mongolia"."age": 18."gender" : false },
{"name" : "Pharmacist Huang"."hometown" : Peach Blossom Island."age": 40."gender" : true },
{"name" : "Broken jade"."hometown" : "Dali"."age": 16."gender" : true },
{"name" : "Lord Duan"."hometown" : "Dali"."age" : 45, "gender" : true },
{"name" : "Hong Qi Gong"."hometown" : "Hua zheng"."age": 18."gender" : true }])
Copy the code

3.1 Simple Query

  • Method find() : query

    Db. Collection name.find ({conditional document})

  • Method findOne() : query, returning only the first one

    Db. Collection name. FindOne ({condition document})

  • Method pretty() : format the result; Cannot be used with findOne()!

    Db. Collection name. Find ({condition document}).pretty()

3.2 Comparison operators

  • Equal: The default is equal to judgment, no operator
  • Less than:$lt (less than)
  • Less than or equal to:$LTE (less than equal)
  • More than:$gt (greater than)
  • Greater than or equal to:$gte
  • Is not equal to:$ne
Db.stu. find({age:{db.stu.find({age:{);$gte18}}) :Copy the code

3.3 Logical operators

Logical operators mainly refer to and and or logic

  • And: Write multiple conditions in JSON
Query whether the age is 18 or greater and gender istrueStudents in the db. Stu. Find ({age: {$gte:18},gender:true})
Copy the code
  • Or: Use $or, the value is an array, each element in the array is JSON
Query age > 18 or genderfalseStudents in the db. Stu. Find ({$or:[{age:{$gt:18}},{gender:false}]}) query older than 18 or gender for boys, and name is guo jing db. Stu. Find ({$or:[{age:{$gte:18}},{gender:true}],name:'gj'})
Copy the code

3.4 Range operators

Using $in, $nin determines if the data is in an array

Query the ages of 18 and 28 students db. The stu. Find ({age: {$in: [18,28,38]}})Copy the code

3.5 Regular expressions are supported

Write regular expressions using $regex

The query name in'yellow'Db.stu. Find ({name:{db.stu.$regex:'^ yellow'}})
Copy the code

3.6 Custom Query

Mongo shell is a JS execution environment that uses $WHERE to write a function that returns data that meets the criteria

Find ({db.stu.find({db.stu.find);$where:function() {
     return this.age>30;}
})
Copy the code

3.7 the skip and limit

  • The limit() method reads a specified number of documents
Db.stu.find ().limit(2)Copy the code
  • Method skip() : Used to skip a specified number of documents
Db.stu.find ().skip(NUMBER) d.stu.find ().skip(2)Copy the code
  • At the same time use
db.stu.find().limit(4).skip(5)
db.stu.find().skip(5).limit(4)
Copy the code

Note: Using skip first is more efficient than using limit

3.8 the projection

Select only the required fields in the query result

Find ({},{field name :1,… })

The parameters are field and value. A value of 1 indicates display, and a value of 0 indicates no display.

  • The _id column is displayed by default. If the _id column is not displayed, set it to 0
  • It cannot be set to 0 for other fields that are not displayed

db.stu.find({},{_id:0,name:1,gender:1})

3.9 the sorting

The sort() method is used to sort the query results by the specified field

Db.set name.find ().sort({field :1,… })

Parameter 1 is in ascending order. Parameter -1 is in descending order

Db.stu.find ().sort({gender:-1,age:1})Copy the code

3.10 Collecting Statistics

The count() method is used to count the number of documents in the result set

Set name find({condition}).count() db Collection name.count ({condition})

db.stu.find({gender:true}).count()
db.stu.count({age:{$gt:20},gender:true})
Copy the code

4 mongodb updates

Update ({query}, {update}, {multi: Boolean})Copy the code
  • Parameter query: indicates the query condition
  • Parameter update: Update operator
  • Multi: This parameter is optional. The default value is false, indicating that only the first data found is updated. The value of true indicates that all data that meet the conditions are updated
db.stu.update({name:'hr'},{name:'mnc'})           Update the entire document
db.stu.update({name:'hr'}, {$set:{name:'hys'}})    # specify a key update operation
db.stu.update({},{$set:{gender:0}},{multi:true})  # update all
Copy the code

Note: “Multi Update only works with $operators”

  • Multi parameter must be used with $set!

5 Delete mongodb

Remove ({query}, {justOne: Boolean})Copy the code
- Parameter query: indicates the conditions of the document to be deleted. - Parameter justOne: indicates that only one document is deleted if it is set to true or 1Copy the code

summary

  1. Insert ({data}) db. Save ({complete data with _id}) # Save for the specified _id, update if present, insert if not present
  2. Remove ({condition}, {justOne: true/false})
  3. Update ({condition}, {$set:{complete data/partial data}}, {multi: true/false})
  4. Find ({condition}, {field projection})