News Ticker

Getting started

You will need to download and install MongoDB, create the database and collections. Lets get started:

1: Download and install MongoDB

2: Create the database and collections

  • Start MongoDB and the Shell and enter the following commands to create the database and at least one collection:
  1. Create the database: use databasename
  2. Create the two collection: db.createCollection("collectionname")

Verify that all has worked:

  1. List all databases: show dbs
    • Output: A list of databases including at least local, maybe test and if all went well databasename
  2. Select your database: use databasename
    • Output: Switched to db databasename
  3. Show all collections: show collections
    • Output: Two system collection system.indexes and system.profile and if all went well one collection collectionname

3: POM dependencies

We need the MongoDB driver and Morphia. Update your POM from the repository and run it in order to download the required dependencies.

4: Importing data.

You may have data that has been extracted from another database. If so you can import it into the newly created collection. These instructions assume that the data you want to import is on JSON format. If not more detailed instructions on how to import data in XML and CSV format can be found on the MongoDB website.

Importing directly into MongoDB using the mongoimport utility.

  1. Open up a command prompt and change directories to the bin directory where you installed mongoDB. By default it will be C:\mongodb\bin
  2. Launch the import utility by typing: mongoimport at the command prompt.
  3. Copy the file containing the JSON data the you want to import into the bin directory.
  4. Run the following command: mongoimport --db databasename --collection collectionname --file filename.json --dbpath C:\data\db

Explanation of switches

--db databasename – the name of the target database

--collection collectionname – the name of the collection in which to import the data

--file filename.json – the location of the source file containing the JSON data

--dbpath C:\data\db – the location of the data

Leave a Reply