MongoDb – Queries

Sample Document
Git

{
    "index": NumberInt(0),
    "name": "Aurelia Gonzales",
    "isActive": false,
    "registered": ISODate("2015-02-11T04:22:39+0000"),
    "age": NumberInt(20),
    "gender": "female",
    "eyeColor": "green",
    "favoriteFruit": "banana",
    "company": {
      "title": "YURTURE",
      "email": "aureliagonzales@yurture.com",
      "phone": "+1 (940) 501-3963",
      "location": {
        "country": "USA",
        "address": "694 Hewes Street"
      }
    },
    "tags": [
      "enim",
      "id",
      "velit",
      "ad",
      "consequat"
    ]
  }

1. Insert Many :

db.getCollection('Persons').insertMany( [{},{},{}])

2. Empty Query:

db.Persons.find({})

3. Equality Query:

Operators

4. Comparison Operator:

db.Persons.find({
        "eyeColor" : {"$ne" : "green"}
    })
// Sort By Name
db.Persons.find({
        "name" : {"$gt" : "N"}
    })
    .sort({
        "name":1
     })
//Find By Date
db.Persons.find({
        "registered" : { "$gt" : ISODate("2015-02-11T04:22:39.000Z") }
    })

5. Comparison Operator: Used In Array of Values

db.Persons.find({
        "age" : { "$in" : [20,22] }
    })

If you use query with different name , the implicit and will work. If we use same field we have to use Explicit $and operator.

db.Persons.find({
        $and : [ {gender: "female"}, {favoriteFruit: "banana"} ]
    })
//Implicity 
db.Persons.find({
        gender: "female", favoriteFruit: "banana"
    })

Same Field

db.Persons.find({
        $and: [ {age: {$ne: 25}}, {age: {$gte: 25}} ]
    })
    .sort({ age: 1 })
//
db.Persons.find({
         age: {$ne: 25}, age: {$gte: 25}
     })
    .sort({ age: 1 })

6. OR Operator

db.Persons.find({
         $or: [{isActive: true}, {eyeColor: "blue"}]
     })

Different Value of the Same Field

db.Persons.find({
         eyeColor: {$in: ["green", "blue"]}
     })

7. Query Embedded Document

db.Persons.find({
         "company.location.country":"USA",
         "company.title": "YURTURE"
     })

8. Query Array by Field Value

db.Persons.find({
         "tags.1":"ad"
     })

9.Query Array $all, $size

10. Querying Array of Nested Document

{
  "name": "Mike",
  "friends": [
    {
      "name": "Lora",
      "age": NumberInt(23),
      "registered": true
    },
    {
      "name": "Bob",
      "age": NumberInt(25),
      "registered": false
    },
    {
      "name": "Steve",
      "age": NumberInt(27)
    }
  ]
}
db.first.find({
    "friends.name":"Lora"
    })
//
db.first.find({
    "friends.age": {$gt: 20, $lt:28}
    })
// Order is important here - Result 0
db.first.find({
    friends: {
        "age":27,
        "name": "Steve"
        }
    })
//Result 1 
db.first.find({
    "friends.name":"Steve",
    "friends.age":27
    })

11. Element match Used to update specific document in array
Order does not matter here.

db.first.find({
    friends: {$elemMatch: {"age":23, "name":"Lora"}}
    })

12. Element Operators

db.Persons.find({
    surname: {$exists: false}
    })
//Type Match 
db.Persons.find({
    index: {$type: "int", $eq: 10}
    })

13. Fields Filtering

db.Persons.find({
    name: 1, age: 1, eyeColor: 1, "company.location":1
    })

14.Regex

db.Persons.find({
    name: {$regex: /rel/i}
    })
//
db.Persons.find({
    name: {$regex: /rel/, $options: "i"}
    })

Reference

MongoDb Queries

Leave a Reply

Your email address will not be published. Required fields are marked *