Friday 1 December 2017

MongoDB Basic Commands

Herei am taking bike as an collection name


1. update 
db.bike.update({"operationCode":"9102124"},{$set:{"operationCode":"9102125"}})


2. sort
   db.bike.find().sort({"_id":-1})

3. Query
db.bike.find().pretty()

4.find
db.bike.find({"priority":4}).pretty()

5. and, or

db.bike.find( { $and:[ {"priority":3},{"priority":4}]}).pretty()

db.bike.find( { $or:[ {"priority":3},{"priority":4}]}).pretty()

6. limit
db.bike.find().limit(2)

7. index
db.bike.ensureIndex({dealerID:1})

8. bulk write
db.bike.bulkWrite([{insertOne:{“customerID”:232}},{deleteOne:{“_id”:433}}])

db.bike.bulkWrite(
      [
         { insertOne :
            {
               "document" :
               {
                  "_id" : 4,                }
            }
         },
         { insertOne :
            {
               "document" :
               {
                  "_id" : 5, 
               }
            }
         },
         { updateOne :
            {
               "filter" : { "customerID" : 24 },
               "update" : { $set : { "customerID" : 2213 } }
            }
         },
         { deleteOne :
            { "filter" : { "customerID" : 24} }
         },
      ]
   );

9. copy to another collection
db.bike.copyTo(appointmentID)

10. count
db.bike.find().count()

11.distinct
db.bike.distinct(“dealerID”)

12. explain
db.bike.explain()

13.validate
db.bike.validate(true)

14.total index size
db.bike.totalIndexSize()

15.total size
db.bike.totalSize()

16.storage size
db.bike.storageSize()

17. stats
db.bike.stats()

18. latency stats
db.bike.latencyStats( { histograms: true } ).pretty()

19. get index
db.bike.getIndexes()

20. create database
use database name

21. create collection 
db.createCollection(“collectionne”)

22 insert data
db.bike.insert({
...     "_id" : "4333”,
...     "repairOrderID" : "",
...     "customerID" : "24",
...     "serviceAdvisorID" : "TEK00",
...     "isWalkin" : false,
...     "appointmentDateTime" : ISODate("2017-09-04T14:30:00.000Z"),
...     "appointmentCreateDate" : ISODate("2017-09-04T08:07:31.552Z"),
...     "appointmentCreatedSource" : "",
...     "appointmentCreatedBy" : "",
...     "advisorNotes" : "",
...     "VIN" : "",
...     "vehicleID" : "73aad8c2-9148-11e7-8000-000000000000",
...     "year" : 2017,
...     "make" : "GMC",
...     "model" : "Sierra 1500",
...     "status" : 0,
...     "serviceSelected" : [],
...     "customerComment" : "",
...     "dealerID" : "5",
...     "isActive" : false,
...     "lastUpdatedByUser" : "",
...     "lastUpdatedByDateTime" : Date(-62135596800000),
...     "lastUpdatedByDisplay" : "",
...     "documentVersion" : 0.0
... })

23 drop database
drop.testdb()

24 drop collection
drop.bike()

25 drop document
db.bike.remove({“_id”:4333})

26 selecting certain fields
db.bike.find({“_id”:”1”}).pretty()

27 select array objects
db.bike.find({“_id”:”3”},{vehicalDamage:{$elemMatch:{“vehicalDamageID”:”1”,”damageType”:”Scratch”}}})







No comments:

Post a Comment