Tuesday, August 20, 2019

Basic Mongodb Commands

Query to insert a document:

> db.goo.insert({_id:4})
WriteResult({ "nInserted" : 1 })
>

++++++++++++++++++++++++++++++++++++++++++++
Query to select what we inserted

> db.goo.find()
{ "_id" : 4 }
>
+++++++++++++++++++++++++++++++++++++++++++++++++
If we try to insert same data again, mongo complains.


> db.goo.insert({_id:4})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 11000,
                "errmsg" : "E11000 duplicate key error collection: demo.goo index: _id_ dup key: { : 4.0 }"
        }
})
>

++++++++++++++++++++++++++++++++++++++++++++++++++++
you cant insert the document with same ID but you can save it , using save ()

> db.goo.save({_id:4,x:1})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
> db.goo.find()
{ "_id" : 4, "x" : 1 }

> db.goo.save({_id:4,x:1,y:2})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.goo.find()
{ "_id" : 4, "x" : 1, "y" : 2 }
>

Lets save if i want to change the value of y.

> db.goo.save({_id:4,y:false})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.goo.find()
{ "_id" : 4, "y" : false }
>

Hey,,,, save have replace the document not updated the Y field.

Save is use to replace the document

++++++++++++++++++++++++++++++++++++++

Search doc:

-> Find a document with ID 4
> db.goo.find({_id:4})
{ "_id" : 4, "y" : false }
>

-> Find a doc with y= false

> db.goo.find({y:false})
{ "_id" : 4, "y" : false }

>> Find all doc with id=4 and y=false
> db.goo.find({_id:4,y:false})
{ "_id" : 4, "y" : false }
>

>> Find id such it is greater than 1 and less than 7
> db.goo.find({_id:{$gt:1,$lt:7}})
{ "_id" : 4, "y" : false }
>
++++++++++++++++++++++++++++++++

Updates - it does not replace the value as save()
++++++++++++++++++++++++++++++++++++
>> Find a document whoes ID is 4 and set x=1

> db.goo.update({_id:4},{$set:{x:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.goo.find()
{ "_id" : 4, "y" : false, "x" : 1 }
>
> db.goo.update({_id:4},{$set:{y:true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.goo.find()
{ "_id" : 4, "y" : true, "x" : 1 }
>

Not just ID you can look for other criteria and update, here we wil increment the val by 1.

> db.goo.update({y:true},{$inc:{x:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.goo.find()
{ "_id" : 4, "y" : true, "x" : 2 }
>


++++++++++++++++++++++++++++++

to describe a collections

db.goo.stats()

+++++++++++++++++++++++++++++

Delete
+++++++++++++++++++++
Delete a id where id=4

> db.goo.remove({_id:4})
WriteResult({ "nRemoved" : 1 })
> db.foo.find()
>

To delete the collections

> db.goo.drop()
true
> show collections
>

Drop a database

> db
demo
> db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }
> db
demo
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
movie   0.000GB
>



+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
BSON - Binary Serialization object notation
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


> use demo
switched to db demo
> db
demo
>

> var p={name:'dude',born:ISODate("1984-04-01"),likes:['naps','cake'],points:1}
> db.peeps.save(p)
WriteResult({ "nInserted" : 1 })
> db.peeps.find()
{ "_id" : ObjectId("5d52b9237aa191d958d087e3"), "name" : "dude", "born" : ISODate("1984-04-01T00:00:00Z"), "likes" : [ "naps", "cake" ], "points" : 1 }
>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

Sort:

++++++++++++++
Here is a collections

Sort Y in Descending order

Sort first by Y then by X (here sort y ascending then X ascending)



Flip that say Y ascending and X descending


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Limit: From millions of documents, if i want to display on few of them


When i want only last 2 document, i dont want to see miiliions of record.
Note:  sort helps to arrange the data in Asc or Dsc and then fetch the data

> db.mycol.find().sort({_id:1}).limit(2)
{ "_id" : ObjectId("5d5a796833fcf969c786e4fc"), "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by" : "tutorials point", "url" : "http://www.tutorialspoint.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5d5a79c633fcf969c786e4fd"), "title" : "MongoDB Mideocre", "description" : "MongoDB is robust database", "by" : "tutorials point", "url" : "http://www.testurl1.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home