0% found this document useful (0 votes)
95 views12 pages

Mongodb Lab 7

Uploaded by

Leya Sri
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)
95 views12 pages

Mongodb Lab 7

Uploaded by

Leya Sri
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/ 12

MongoDB Instasllation

Steps to Install MongoDB on Windows using MSI


To install MongoDB on Windows, first, download the MongoDB server
and then install the MongoDB shell. The Steps below explain the
installation process in detail and provide the required resources for the
smooth download and install MongoDB.

Step 1: Go to the MongoDB Download Center to download the MongoDB


Community Server.

Here, You can select any version, Windows, and package according to
your requirement. For Windows, we need to choose

• Version: 7.0.4
• OS: Windows x64
• Package: msi
Step 2: When the download is complete open the msi file and click
the next button in the startup screen:

Step 3: Now accept the End-User License Agreement and click the next
button:
Step 4: Now select the complete option to install all the program features.
Here, if you can want to install only selected program features and want
to select the location of the installation, then use the Custom option:

Step 5: Select “Run service as Network Service user” and copy the path
of the data directory. Click Next:
Step 6: Click the Install button to start the MongoDB installation process:

Step 7: After clicking on the install button installation of MongoDB


begins:
Step 8: Now click the Finish button to complete the MongoDB
installation process:

Step 9: Now we go to the location where MongoDB installed in step 5 in


your system and copy the bin path:

Step 10: Now, to create an environment variable open system properties


>> Environment Variable >> System variable >> path >> Edit
Environment variable and paste the copied link to your environment
system and click Ok:
Step 11: After setting the environment variable, we will run the
MongoDB server, i.e. mongod. So, open the command prompt and run
the following command:
mongosh
When you run this command you will get an error i.e. C:/data/db/ not
found.

Step 12: Now, Open C drive and create a folder named “data” inside this
folder create another folder named “db”. After creating these folders.
Again open the command prompt and run the following command:
Mongosh

Now, this time the MongoDB server(i.e., mongod) will run successfully.

In command prompt:

Microsoft Windows [Version 10.0.22631.3593]


(c) Microsoft Corporation. All rights reserved.

C:\Users\yeshwanth c>mongosh
Current Mongosh Log ID: 669256c2d474c0d514c4e49a
Connecting to:
mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTim
eoutMS=2000&appName=mongosh+2.2.12
Using MongoDB: 7.0.12
Using Mongosh: 2.2.12

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
The server generated these startup warnings when booting
2024-07-11T14:58:03.546+05:30: Access control is not enabled for
the database. Read and write access to data and configuration is
unrestricted
PERFORM CRUD OPERATIONS:

1.Create Database and Collection:


> db.createCollection("mycollection")

Output:
{ ok: 1 }

2.Create (Insert Documents):

mydatabase> db.mycollection.insertOne({name:"Alice", age:25,city:"New York"})

Output:
acknowledged: true,

insertedId: ObjectId('6690b10ecece137f5bc4e49b')

mydatabase> db.mycollection.insertMany([

... {name:"Bob",age:30,city:"San Fransisco"},

... {name:"Charlie",age:35,city:"Los Angels"}

... ])

Output:
acknowledged: true,

insertedIds: {

'0': ObjectId('6690b18bcece137f5bc4e49c'),

'1': ObjectId('6690b18bcece137f5bc4e49d')
}

3.Read (Find Documents):

mydatabase> db.mycollection.find()

Output:
_id: ObjectId('6690b10ecece137f5bc4e49b'),

name: 'Alice',

age: 25,

city: 'New York'

},

_id: ObjectId('6690b18bcece137f5bc4e49c'),

name: 'Bob',

age: 30,

city: 'San Fransisco'

},

_id: ObjectId('6690b18bcece137f5bc4e49d'),

name: 'Charlie',

age: 35,

city: 'Los Angels'

mydatabase> db.mycollection.find({name:"Alice"})

Output:
_id: ObjectId('6690b10ecece137f5bc4e49b'),

name: 'Alice',

age: 25,

city: 'New York'


mydatabase> db.mycollection.find({age:{$gt:25}})

Output:
_id: ObjectId('6690b18bcece137f5bc4e49c'),

name: 'Bob',

age: 30,

city: 'San Fransisco'

},

_id: ObjectId('6690b18bcece137f5bc4e49d'),

name: 'Charlie',

age: 35,

city: 'Los Angels'

4. Update Documents:
mydatabase> db.mycollection.updateOne(

... {name:"Alice"},

... {$set:{age:26}}

... )

Output:
acknowledged: true,

insertedId: null,

matchedCount: 1,

modifiedCount: 1,

upsertedCount: 0

}
mydatabase> db.mycollection.updateMany(

... {city:"New York"},

... {$set:{city:"NYC"}}

... )

Output:
acknowledged: true,

insertedId: null,

matchedCount: 1,

modifiedCount: 1,

upsertedCount: 0

5.Delete Documents:
mydatabase> db.mycollection.deleteOne({name:"Charlie"})

output:
{ acknowledged: true, deletedCount: 1 }

mydatabase> db.mycollection.deleteMany({city:"NYC"})

Output:
{ acknowledged: true, deletedCount: 1 }

MongoDB Queries:
Basic Queries:
1.Find all Documents:
mydatabase> db.mycollection.find()

Output:
{
_id: ObjectId('6690b18bcece137f5bc4e49c'),

name: 'Bob',

age: 30,

city: 'San Fransisco'

2.Find Document with a specific field:

mydatabase> db.mycollection.find({name:"Bob"})

Output:
{

_id: ObjectId('6690b18bcece137f5bc4e49c'),

name: 'Bob',

age: 30,

city: 'San Fransisco'

mydatabase> db.mycollection.find({age:{$gt:25}})

Output:
{

_id: ObjectId('6690b18bcece137f5bc4e49c'),

name: 'Bob',

age: 30,

city: 'San Fransisco'

4.Projection(Select Specific Field):

mydatabase> db.mycollection.find({},{name:1,_id:0})

Output:
[ { name: 'Bob' } ]
5.Sorting:
mydatabase> db.mycollection.find().sort({age:1})

Output:
{

_id: ObjectId('6690b18bcece137f5bc4e49c'),

name: 'Bob',

age: 30,

city: 'San Fransisco'

You might also like