0% found this document useful (0 votes)
491 views5 pages

MongoDB Guide PDF

This document provides instructions for installing, setting up, running, and working with MongoDB on Windows. It explains how to: 1) Install MongoDB and the MongoDB Compass GUI from the MongoDB download center. 2) Create the default data directory and optionally specify an alternate directory. 3) Start the MongoDB process using mongod.exe and connect to it using Compass. 4) Import JSON data, use the mongo shell to execute queries and scripts, and connect to MongoDB from scripts.

Uploaded by

manthan pandit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
491 views5 pages

MongoDB Guide PDF

This document provides instructions for installing, setting up, running, and working with MongoDB on Windows. It explains how to: 1) Install MongoDB and the MongoDB Compass GUI from the MongoDB download center. 2) Create the default data directory and optionally specify an alternate directory. 3) Start the MongoDB process using mongod.exe and connect to it using Compass. 4) Import JSON data, use the mongo shell to execute queries and scripts, and connect to MongoDB from scripts.

Uploaded by

manthan pandit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Database Systems 236363 Technion – Israel Institute of Technology Winter 2017-2018

Guide for MongoDB (Windows)


In this document we concisely explain how to install, setup, run and work with MongoDB for
Windows. For other platforms, as well as for a more detailed explanation, please check out the
official guides at: ​https://docs.mongodb.com/getting-started/shell/installation/

1. Install MongoDB for Windows1

Download the latest production release of MongoDB from the MongoDB Download Center:
https://www.mongodb.com/download-center#community

1
​For the full reference, go to:
https://docs.mongodb.com/getting-started/shell/tutorial/install-mongodb-on-windows/
Database Systems 236363 Technion – Israel Institute of Technology Winter 2017-2018

During the installation process you will be given the option to install MongoDB Compass, the official GUI
for MongoDB, in addition to MongoDB Server. We advise you to install it.

2. Set up the MongoDB environment

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is the
absolute path \data\db on the drive from which you start MongoDB. Create this folder by running the
following command in a Command Prompt:

> md \data\db

Alternatively, you can specify an alternate path for data files using the --dbpath option to mongod.exe,
for example:

> "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe" --dbpath d:\test\mongodb\data

3. Start MongoDB

To start MongoDB, run mongod.exe. For example, at the Command Prompt, type:

> "C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe"


Database Systems 236363 Technion – Israel Institute of Technology Winter 2017-2018

This starts the main MongoDB database process. The waiting for connections message in the console
output indicates that the mongod.exe process is running successfully. Note: if the data directory does
not exist, the program will exit with an error message.

4. Connect to MongoDB with Compass2

Run Compass by double-clicking the Compass icon appearing in the Desktop. Once Compass is running,
an initial connection dialog appears:

You can connect using the default values by clicking “CONNECT”.

2
​For the full reference, go to: ​https://docs.mongodb.com/compass/current/connect/
Database Systems 236363 Technion – Israel Institute of Technology Winter 2017-2018

5. Work with Mongo

Upload JSON File3


Open the Command Prompt in C:\Program Files\MongoDB\Server\3.6\bin

Type the following command:

> mongoimport --db <db-name> --collection <collection-name> --file <file-path>

Mongo Shell4
The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mongo shell to
query and update data as well as perform administrative operations.

To start the mongo shell and connect to your MongoDB instance running on localhost with default port,
In the command prompt go to C:\Program Files\MongoDB\Server\3.6\bin, and type:

> mongo

To display the database you are using, type db:

> db

To switch databases, issue the use <db> helper, as in the following example:

> use <database>

You can run queries against your database, like so:

> db.myCollection.find( { color: “pink” } );

Execute a JavaScript File5


You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform
administrative operation.

For example, assume you a database called “mydb”. Create a JavaScript file with the name “myscript.js”
that contains the following lines:

3
For the full reference, go to: ​https://docs.mongodb.com/manual/reference/program/mongoimport/
4
For the full reference, go to: ​https://docs.mongodb.com/manual/mongo/
5
For the full reference, go to: ​https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/
Database Systems 236363 Technion – Israel Institute of Technology Winter 2017-2018

conn = new Mongo();


db = conn.getDB("mydb");

var c = db.getCollectionNames();
printjson(c);

To execute the script, run the following command in C:\Program Files\MongoDB\Server\3.6\bin

> mongo localhost:27017/mydb <path-to-myscript.js>

You might also like