MongoDB Installation

  1. Open ACSR for MongoDB, the software will be installed in C:\program files\MongoDB\Server\3.2\bin
  2. Create a new directory C:\data\db , this is the default location where MongoDB will store the data.

Start MongoDB

In command prompt, go to your <mongodb installation dir> and start mongod.exe

> cd C:\program files\MongoDB\Server\3.2\bin

> mongod.exe

Import JSON file to MongoDB using mongoimport

In a new command prompt, go to your <mongodb installation dir> and use mongoimport to import json file into MongoDB with specific database name and collection name.

>cd C:\program files\MongoDB\Server\3.2\bin

>mongoimport --db databaseName --collection TableName --file C:\data.json

Start Mongo Shell

Mongo Shell is where you query, update data, and perform administrative operations.
In a new command prompt, go to your <mongodb installation dir> and start mongo shell

>cd C:\program files\MongoDB\Server\3.2\bin

>mongo

Above will connect to localhost 27017, you can use below to connect to other mongoDB

>mongo -u <user> -p <pass> --host <host> --port 99999

MongoDB Query using Mongo Shell

Use the Mongo Shell command prompt to perform below queries.

Display current database name

 db

Display all database

show dbs

Switch databases: If you switch to a non-existing databases,when you first store data in the database, such as by creating a collection, MongoDB creates the database

use <dbname>
use database1

Display all tables

show tables

Display all json object in a collection

db.<collection>.find() // eg: db.myvcollection1.find()

Display all JSON object with json format

db.myvcollection1.find().pretty()

To display amount of JSON object

db.myvcollection1.find().count()

Return JSON with specific field

db.myvcollection3.find({"_id" : "AVofEWj6dSKkx4cNAoNV"}) 
db.myvcollection3.find({"_id" : "AVofEWj6dSKkx4cNAoNV"}).count() 

SQL equivalent: select * from tbl1 where _id=AVofEWj6dSKkx4cNAoNV

Return JSON with specific field(nested JSON)

db.myvcollection1.find({"_source.host" : "twswcmyazap05"})

Return JSON with Multiple specific fields (AND Operation)

db.myvcollection1.find({"_source.trace" : "254c","_id" : "AVofEWj"}) 

SQL equivalent: select * from tbl1 where _source.trace=254c and _id=AVofEWj

Return only specific fields: By default, _id will return. "_id": {...,"_id": 0} will disable _id return

db.myvcollection1.find({"_type" : "myv_postpay_ms"},{"_score" : 1, "_id": 0}) 

SQL equivalent: select _score from tbl1 where _type=myv_postpay_ms.

And

db.restaurants.find({ $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } )

Like

db.myvcollection1.find({"_source.api_request" : /visiondist/}).count() 
or 
db.myvcollection1.find({"_source.api_request" : /.*visiondist.*/}).count()

Greater Than/ Less Than / Not

db.student.find( { "gpa": { $gt: 3.0 } } ) 
db.student.find( { "gpa": { $lt: 2.5 } } ) 
db.student.find( { "gpa": {$not : { $gt: 3.0 } } )

Sort ( 1 for ascending/ -1 for descending)

db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )

Limited / Skip

db.myvcollection3.find().limit(10) 
db.myvcollection3.find().skip(3) //skip first 3 JSON

results matching ""

    No results matching ""