Getting started
You will need to download and install MongoDB, create the database and collections. Lets get started:
1: Download and install MongoDB
- Download the appropriate version of Mongo for your operating system: Download MongoDB
- Install MongoDB according to the installation instructions for your operating system: Installation Instructions
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:
- Create the database:
use databasename
- Create the two collection:
db.createCollection("collectionname")
Verify that all has worked:
- List all databases:
show dbs
- Output: A list of databases including at least local, maybe test and if all went well databasename
- Select your database:
use databasename
- Output: Switched to db databasename
- 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.
- Open up a command prompt and change directories to the bin directory where you installed mongoDB. By default it will be C:\mongodb\bin
- Launch the import utility by typing:
mongoimport
at the command prompt. - Copy the file containing the JSON data the you want to import into the bin directory.
- 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
– the location of the source file containing the JSON datafilename
.json
--dbpath C:\data\db
– the location of the data
Leave a Reply