How to do that in Mongo

1. Check for not null

db.getCollection('customerToPhaseMapping').find({"regionId":{$ne:null}})

2. Combining conditions using and/or

{
   $and :
             [
                  {condition 1},
                  {condition 2}
             ]
}

{
   $or:
             [
                  {condition 1},
                  {condition 2}
             ]
}

db.getCollection('wellPlanningOpsReport').find({$and:[{"wellUid" : '4212334286'},
{"assemblyInfoList.StringType" : 'Casing'}]})

3. Search all wells for prmr that has wellViewUid

db.getCollection('wells').find({$and : [{"customer" : 'prmr'}, {"wellViewUid" : {$ne : null}}]})

4. Search all wells that does not have wellViewUid

db.getCollection('wells').find({$and : [{"customer" : 'prmr'}, {"wellViewUid" : {$exists: false}}]})

5. Search "like"

db.getCollection('wells').find({"name" : /.*CCU ATLAN.*/})

6. Greater than and Less than search
(Remove last 5 digits from epoch time - Ignore this comment)

db.getCollection('logData').find({ $and : [{'logId' : ObjectId("589f492f3100009700b6cfd1")},{'chunkIndex' : { '$gt' : 14869894 , '$lt' : 14869896 } }]})

7. Get the count of records

db.getCollection('logData').find({ $and : [{'logId' : ObjectId("589f492f3100009700b6cfd1")},{'chunkIndex' : { '$gt' : 14869894 , '$lt' : 14869896 } }]}).count()

8. Update a record

db.getCollection('logData').update({
    "logId": ObjectId("58c5fcee460000100ee4f511"),
    "chunkIndex": { '$gte' : 14893708 , '$lte' : 14893708 },
    "logDataRows.state": "UNKNOWN",
  },
  {"$set": {"logDataRows.$.state": "UP"}}
)

9. in command

{ field: { $in: [, , ... ] } }

10. Order By

db.getCollection('wells').find( {
$and : [
             {"customer" : 'cus1'}, 
             {"regionId" : ObjectId("57f7f6f63f64060f0024d12d")}
]
}).sort({ "createdAt" : -1 })

Asc : 1
Desc : -1

11. To get the Top record from a Mongo collection

db.getCollection('logData').find({"logId" : ObjectId("592d982b1f00003200546509")}).sort({"_id":-1}).limit(1)

12.
db.getCollection('wells').find({rigs:{$elemMatch:{rigId: '593130a61c00002700b16dd5'}}})

13. To check whether a attribute exists in a collection

db.getCollection('rigs').find({"wells" : {$exists:true} })

14. To get distinct values for a field based on certain criteria

db.wells.distinct("regionId", {"customer" : 'prmr'});

15. How to get the mongodb versions

> npm list mongodb-core
node-nextgen@0.0.0 C:\Workspaces\node-nextgen
+-- agenda@1.0.3
| `-- mongodb@2.2.35
|   `-- mongodb-core@2.1.19
+-- mongodb@2.2.26
| `-- mongodb-core@2.1.10
`-- mongoose@4.12.6
  `-- mongodb@2.2.33

    `-- mongodb-core@2.1.17

16. How to retrieve specific fields from a collection

db.getCollection('planningProcessStatus').find({'createdTimestamp' : {'$gt' : 1544256000000, '$lt'  :1544331600000 } },  { createdTimestamp: 1 })


17. How to retrieve specific fields from a collection with _id suppressed


db.getCollection('planningProcessStatus').find({'createdTimestamp' : {'$gt' : 1544256000000, '$lt'  :1544331600000 } },  { createdTimestamp: 1 , _id: 0})


18. Searching with ISODates

db.photoFiles.find({$and: [{"size" : {$gte : 1000000}},{"uploadDate" : {$gte: ISODate('2020-07-01T21:31:45.133-05:00')}}]})
   .projection({})
   .sort({_id:-1})
   .limit(100)

19. Delete multiple records

db.getCollection('wellPlanningOpsReport').remove({"wellUid" : {'$in' : ['3712928936', '3712928967']}})

1 comment:

Anonymous said...

Good one..

 My Attempt to learn AI  GPT Generative Pretrained Transformer Created by Google in 2017 with the publication of the paper "Attention ...

Popular in last 30 days