Data Query in MongoDB

Preparing data

db.stu.insert({name:'Guo Jing',hometown:'Mongolia',age:20,gender:true})
db.stu.insert({name:'Huang Rong',hometown:'Taohua Island',age:18,gender:false})
db.stu.insert({name:'Yang Kang',hometown:'daikin',age:20,gender:true})
db.stu.insert({name:'Mu Nianchi',hometown:'Song',age:18,gender:false})
db.stu.insert({name:'evil east',hometown:'Taohua Island',age:40,gender:true})
db.stu.insert({name:'West Poison',hometown:'Western Regions',age:41,gender:true})
db.stu.insert({name:'Southern Imperial',hometown:'Dali',age:42,gender:true})
db.stu.insert({name:'Northern Beggar',hometown:'Song',age:45,gender:true})
db.stu.insert({name:'Small armor',hometown:'Song',age:45,gender:false})
db.stu.insert({name:'Central Divinity',hometown:'quanzhen sect',age:44,gender:true})

Basic Query

  • Method find()

    db. Collection name. find({condition document})
  • Method findOne() query, returning only the first

    db. Collection name. findOne({conditional document})

  • Method pretty() formats the result

    db. Collection name. find({conditional document}). pretty()

Comparison operator

  • Equivalent, default is equal to judgement, no operator

    db.stu.find({age:18}) //Query all ages18Students
  • Less than: $lt

    db.stu.find({age:{$lt:18}})  //Query students younger than 18
  • Less than or equal to: $lte

    db.stu.find({age:{$lte:18}}) //Inquiry for students younger than or equal to 18
  • Greater than: $gt

    db.stu.find({age:{$gt:18}})  //Search for students older than 18
  • Greater than or equal to: $gte

    db.stu.find({age:{$gte:18}})  //Students with queries greater than or equal to 18
  • Not equal to: $ne

    db.stu.find({age:{$ne:18}})   //Inquiry age is not equal to 18 students

Logical Operator

  • There are many conditions when querying, and the connection between them needs to be done by logical operators.

  • Logic and: The default is the relationship between logic and

    // Query age greater than or equal to18,And the gender istrueStudents
    db.stu.find({age:{$gte:18},gender:true})

  • Logic or: Use $or, with an array of values, each element in the array is json

    // Query age is older than18,Or sexfalseStudents
    db.stu.find({$or:[{age:{$gt:18}},{gender:false}]})

  • and with or

    // Query age is older than18Or sextrueThe student, and the student's name is Guo Jing.
    db.stu.find({$or:[{age:{$ge:18}},{gender:true}],name:'Guo Jing'})

Range operator

  • Use $in,$nin to determine whether or not within a certain range

    // Query age is18,28Students
    db.stu.find({age:{$in:[18,28]}})

Regular Expression Query

  • The terminal shell provided by mongodb is also an executor of js, which can write JS code and functions

  • Use //or $regex to write regular expressions

    // Inquiry for classmates surnamed Huang
    db.stu.find({name:/^yellow/})
    db.stu.find({name:{$regex:'^yellow'}})

Custom Query

  • Write a function after $where to return data that meets the criteria

    db.stu.find(
    {
        $where: function(){
          return this.age>30
        }
    }
    )

Limit and skip

  • limit(): Used to read a specified number of documents

    db. Collection name. find().limit(number)
    • Parameter number: Indicates the number of entries to read the document
    • Display all documents in the collection without specifying Number
    // Show the first two students'information
    db.stu.find().limit(3)

  • Skip: Used to skip a specified number of documents

    db. Collection name. find().skip(number)
    • The parameter number represents the number of records skipped, with a default value of 0
  • limit and skip can be used together, regardless of order.

Projection

  • In the return result of the query, only the necessary fields are selected, not the whole field of a document.

  • Grammar:

    // The parameters are fields and values, the value is 1 for display, and the value is 0 for non-display.
    db. Collection name. find({}, {field name: 1,...})
  • For fields that need to be displayed, set to 1, or not to display

  • Special: For the _id column, it is displayed by default. If it is not displayed, it needs to be explicitly set to 0.

sort

  • Method sort(), which is used to sort the result set

    db. Collection name. find().sort({field: 1,...})
  • Parametric 1 is ascending order

  • Parameter - 1 is descending order

    // Sort age from small to large
    db.stu.find({},{name:1,age:1}).sort({age:1})

Number of Statistics

  • Method count() is used to count the number of documents in the result set

    db. Collection name. find({condition}). count()
    db. Collection name. count({condition})
  • Example

    // Statistical age is greater than18 Total number
    db.stu.find({age:{$gt:18}}).count()
    db.stu.count({age:{$gt:18}})

Eliminate duplication

  • Method distinct() to de-duplicate data

    db. Collection name. distinct('de-duplicate field', {condition})
  • Example:

    // Look for students older than 18 and which provinces they come from
    db.stu.distinct('hometown',{age:{$gt:18}})

Keywords: less JSON shell MongoDB

Added by zeropaid on Mon, 10 Jun 2019 01:56:14 +0300