MongoDB

  • A Mongo system holds a set of databases
  • A database holds a set of collections (similar to tables in relational databases)
  • A collection holds a set of documents (think of them as records in relational databases)
  • A document consists of a set of fields
  • A field is a key-value pair (think of the field as a cell, intersection of a table row and a column, where the key is like a column name)
  • A field key must be a string
  • A field value can be a number (integer, float), timestamp, a JSON document, binary element, and an array of values. You can also use boolean values(true and false), but by default they are interpreted and returned by MongoDB as integers (1 for true, 0 for false).
  • Note that databases and collections of documents are lazily created when the first document is saved in them.

  • MongoDB is a self-contained system with no system dependencies. It is installed by unzipping the distribution archive in the directory of your choice. We will refer to this location as MONGO_HOME.

  • The MongoDB database process is launched by a call to the MONGO_HOME\bin\mongod.exe file. Execute the following command:

    mongod --rest
    

    This command will start the MongoDB process and will also activate the RESTful end point on the admin web console.

MongoDB requires a data folder to store its database files. By default, on Windows platform, the location for the MongoDB data directory is C:\data\db. You can use another location, which is done by passing the location as a --dbpath configuration option, e.g.

mongod.exe --dbpath c:\projectX\data

Port 27017 is used for local process connections (our shell will connect to MongoDB via this port), and port 28017 is used to access admin web console using browser.

Open Google Chrome browser and navigate to http://localhost:28017 The Admin Web Console should open. The admin console is rather minimalistic in its UI design and provides a view of a variety of system parameters.

  1. The MongoDB shell MONGO_HOME\bin\mongo.exe which, by default, will try to connect to mongod.exe on the localhost interface and port 27017. Execute this command:

    mongo
    

    This command will start the MongoDB shell, connect to the MongoDB process on port 27017 and return the command prompt '>'. Note: You quit the shell by typing the exit command. By default, you will be connected to the test db.

  2. CLI

create a new database : use MyTest

db.collection1.save( { key1 : 101, key2 : "string value for key2" } ) create and save a JSON document identified between the {} braces and passed as a parameter to the save() method into a collection named collection1 in the current database (currently, MyTest). The current database is aliased as db.

db.collection1.find()

db.your_collection.find({"_id" : ObjectId("<the object id value>") })

db.collection1.stats()

db.system.indexes.find()

results matching ""

    No results matching ""