MongoDB : Exporting JSON with mongoexport

This will export a JSON representation of the database. Note that as a rule – particularly for backing up or moving data – MongoDB recommends the “dump and restore” approach as BSON can contain more rich data than JSON. Nevertheless, mongoexport still has its uses, sometimes a JSON representation of the data is very useful – it’s what we’ve been using so far in the application development.

Basic output to console

Need to specify the name of the database –db and the collection –collection to export. Restoring dumped data with MongoRestore Inserts only, no updates Exporting JSON with mongoexport

Basic output to console

> mongoexport –db meantest –collection tech

Send to a file

> mongoexport –db meantest –collection tech –out MEAN/api/data/tech.json

Create as array

> mongoexport –db meantest –collection tech –out MEAN/api/data/tech.json –jsonArray

Make output pretty

> mongoexport –db meantest –collection tech –out MEAN/api/data/tech.json –jsonArray –pretty

 

MongoDB : How to take export a database using mongo dump

We need to take database backup or have to export specific database in  mongo

This command will export specific databases in mongo and export to home folder /dump

> mongodump –db testDatabase

# to compress in zip file and folder

> mongodump –db testDatabase   –gzip

> cd ~/dump

This command will restore or import specific db

>mongorestore –db testDatabase   –gzip  ~/dump/testDatabase

Note :

There command should be run from command prompt / shell and not from mongo sheel.

Mongo restroe does not update existing collections

MongoDB : Normalize Database reference (DBRefs)

The joy of a Document database is that it eliminates lots of Joins. Your first instinct should be to place as much in a single document as you can. Because MongoDB documents have structure, and because you can efficiently query within that structure there is no immediate need to normalize data like you would in SQL. In particular any data that is not useful apart from its parent document should be part of the same document.

This is not so much a “storage space” issue as it is a “data consistency” issue. If many records will refer to the same data it is more efficient and less error prone to update a single record and keep references to it in other places.

DBRef documents resemble the following document:

{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }

Consider a document from a collection that stored a DBRef in a creator field:

{
  "_id" : ObjectId("5126bbf64aed4daf9e2ab771"),
  // .. application fields
  "creator" : {
                  "$ref" : "creators",
                  "$id" : ObjectId("5126bc054aed4daf9e2ab772"),
                  "$db" : "users"
               }
}

The DBRef in this example points to a document in the creators collection of the users database that has ObjectId("5126bc054aed4daf9e2ab772") in its _id field.

Consider the following operation to insert two documents, using the _id field of the first document as a reference in the second document:

original_id = ObjectId()

db.places.insert({
    "_id": original_id,
    "name": "Broadway Center",
    "url": "bc.example.net"
})

db.people.insert({
    "name": "Erin",
    "places_id": original_id,
    "url":  "bc.example.net/Erin"
})

Then, when a query returns the document from the people collection you can, if needed, make a second query for the document referenced by the places_id field in the places collection.

Reference link for details : https://docs.mongodb.com/manual/reference/database-references/

Certification : MongoDB for Database Adminstrators – M102

Hi Friends,

I feel pleasure to share that I have taken one more step towards MongoDb expertise by successfully finishing yet another certification course for Mongo DB as database administrator 😉 Previous certificate was M101P which was focused on developer’s aspect of mongoDb, this certificate is M102 – which was focused on database administrator’s aspect of MongoDb.
DBA_Database_Administrator_certificate_for_mongo_DB_Aseem_Jain_2016
It was a 2 month long course and I finished this with 90%.
This certificates enabled me to set up production level replica sets, shards, fail overs, indexes, performance tuneups, back up and recovery, data migration and several other complex tasks.

Mac : Installing Mysql on mac using brew

Look, if you just want to try out mysql only for learning then there is not need for local installation, you can try it out free at this site : http://demo.phpmyadmin.net/master-config/

If you want to install Mysql on you mac machine then follow below steps :

  1. Install mysql with brew install mysql
  2. Start mysql with mysql.server start
  3. Connect mysql with mysql -uroot 
  4. Stop mysql with mysql.server stop

 

Note :

To have launchd start mysql at login:

  ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

Then to load mysql now:

  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Or, if you don’t want/need launchctl, you can just run:

$   mysql.server start

$ mysql -uroot

mysql.server stop

Once Mysql is installed you can check port number using below command

mysql> SHOW GLOBAL VARIABLES LIKE ‘PORT’;

+—————+——-+

| Variable_name | Value |

+—————+——-+

| port          | 3306  |

+—————+——-+

1 row in set (0.00 sec)

MongoDB : mongovue a windows desktop GUI client for easy no sql data visualization

http://www.mongovue.com/

MongoVUE is an innovative MongoDB desktop application for Windows OS that gives you an elegant and highly usable GUI interface to work with MongoDB. Now there is one less worry in managing your web-scale data.

MongoVUE makes it a very simple to see and visualize your data. It gives you 3 different views of it – TreeView, TableView and TextView. If you are from RDBMS (SQL) back ground you will feel at home with Table View of MongoDB (no sql).

 

Python : How to covert python dictionary into json and why we need that

Problem

You want to read or write data encoded as JSON (JavaScript Object Notation).

Solution

The json module provides an easy way to encode and decode data in JSON. The two main functions are json.dumps() and json.loads(), mirroring the interface used in other serialization libraries, such as pickle. Here is how you turn a Python data structure into JSON:

import json

data = {
   'name' : 'ACME',
   'shares' : 100,
   'price' : 542.23
}

json_str = json.dumps(data)

Here is how you turn a JSON-encoded string back into a Python data structure:

data = json.loads(json_str)

If you are working with files instead of strings, you can alternatively usejson.dump() and json.load() to encode and decode JSON data. For example:

# Writing JSON data
with open('data.json', 'w') as f:
     json.dump(data, f)

# Reading data back
with open('data.json', 'r') as f:
     data = json.load(f)

Dictionary and json are not same, so when dealing with web application in python we need to convert the python dictonay into json and visa vera. json.dumps and json.loads are used for the same.

# json dumps takes dict as input and return the json object as string.

# json loads takes a json object / string and returns the dict.

 

The sample console output , while trying to get value out on dumped dict :

>> import json
>>> a = {‘foo’: 3}
>>> json.dumps(a)
‘{“foo”: 3}’
>>> obj = json.dumps(a)
>>> print obj
{“foo”: 3}

>>> isinstance(obj,str)
True
>>> a[‘foo’]
3
>>> obj[‘foo’]
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: string indices must be integers, not str
>>> dict = json.loads(obj)
>>> dict[‘foo’]
3