Mongo DB query examples

Greater than:

db.getCollection('StorageContent').find({ $and:[
    {"shardKey" : "7507501"},
    {"cd": {$gte: ISODate("2017-07-02T00:00:00.731Z")}}
]})

Less than:

db.getCollection('NotificationProfile').find({ $and:[
    {createdBy: "hban"}, 
    {deliveryType: "F"}, 
    { 
    "createdDate": {
        $lte: ISODate("2018-11-30")
    }}
]})

Between:

db.getCollection('LogEntry').find({ $and:[
    {
    "time": {
        $gte: ISODate("2017-11-10")
    }}, 
    { 
    "time": {
        $lte: ISODate("2017-11-17")
    }}
]})

Oerder By date:

db.getCollection('notifyEmailSent').find({ $and:[
    {
    "created": {
        $gte: ISODate("2018-12-01 16:00:00:000Z")
    }}
]}).sort({"created": -1})

Exist:

db.getCollection('NotificationProfile').find({ $and:[
    {
        "terminal":{$exists:true}
    },
    {
        "company": '100'
    }
]})

Group By:

db.getCollection('DocumentAudit').aggregate([  
   {  
      "$match":{  
         "tp":"STR",
         "stp":"SSC",
      }
   },
   {  
      "$group":{  
         _id:{  
            "grp":"$grp",
            "com":"$com",
            "tp":"$tp",
            "stp":"$stp"
         },
         count:{  
            $sum:1
         }
      }
   },
   {  
      "$sort":{  
         "_id.grp":1
      }
   },
   {  
      "$sort":{  
         "_id.com":1
      }
   },
   {  
      "$sort":{  
         "_id.tp":1
      }
   },
   {  
      "$project":{  
         "count":1,
         "group":"$_id.grp",
         "company":"$_id.com",
         "type":"$_id.tp",
         "subType":"$_id.stp"
      }
   },
   {  
      "$limit":2000
   }
])

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.