Wednesday, March 30, 2016

MONGO DB BASICS

*******************************
LOGIN INTO MONGO DATABASE
*******************************

C:\MongoDB\bin>mongo.exe
2016-03-30T10:31:31.317-0500 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
MongoDB shell version: 3.2.4
connecting to: test
> show dbs
local  0.000GB

*******************************
USE MYDB DATABASE
*******************************

> use mydb
switched to db mydb
> show dbs
local  0.000GB
> db
mydb

**********************************
Insert into MYDB
**********************************
> db.movie.insert({"name":"tutorials point"})
WriteResult({ "nInserted" : 1 })
> show dbs
local  0.000GB
mydb   0.000GB

*******************************
DROP MYDB DATABASE
*******************************

If you want to drop a database in MONGO DB <mydb>, then dropDatabase() command would be as follows:

1. change your current DB to MYDB
2. Drop using below cmd.

> use mydb
switched to db mydb

> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
> show dbs
local  0.000GB
>

*******************************
CHECK MONGODB VERSION
*******************************


> db.version()
3.2.4

Wednesday, July 22, 2015

Querying data from Mongodb



-- limit output where position : Center results  --

db.player.find(
{"position" : "Center"}

)

-- limit output where position : Center results & display only name values --

db.player.find(
{"position" : "Center"},
{"name":1}

)


-- limit output to 3 results --

db.player.find(
{"position" : "Center"},
{"name":1,_id:0}
).limit(3)

-- skip first 3 results --

db.player.find(
{"position" : "Center"},
{"name":1,_id:0}
).skip(3)


-- output to 2 values per doc & search criteria is filtered by position "or" id --

db.player.find(
{
  $or:[{"position":"Right Wing"},
  {"id":8475761}]
},{"name":1}
).pretty()