Mongodb Lab 7
Mongodb Lab 7
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 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:
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
------
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:
Output:
{ ok: 1 }
Output:
acknowledged: true,
insertedId: ObjectId('6690b10ecece137f5bc4e49b')
mydatabase> db.mycollection.insertMany([
... ])
Output:
acknowledged: true,
insertedIds: {
'0': ObjectId('6690b18bcece137f5bc4e49c'),
'1': ObjectId('6690b18bcece137f5bc4e49d')
}
mydatabase> db.mycollection.find()
Output:
_id: ObjectId('6690b10ecece137f5bc4e49b'),
name: 'Alice',
age: 25,
},
_id: ObjectId('6690b18bcece137f5bc4e49c'),
name: 'Bob',
age: 30,
},
_id: ObjectId('6690b18bcece137f5bc4e49d'),
name: 'Charlie',
age: 35,
mydatabase> db.mycollection.find({name:"Alice"})
Output:
_id: ObjectId('6690b10ecece137f5bc4e49b'),
name: 'Alice',
age: 25,
Output:
_id: ObjectId('6690b18bcece137f5bc4e49c'),
name: 'Bob',
age: 30,
},
_id: ObjectId('6690b18bcece137f5bc4e49d'),
name: 'Charlie',
age: 35,
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(
... {$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,
mydatabase> db.mycollection.find({name:"Bob"})
Output:
{
_id: ObjectId('6690b18bcece137f5bc4e49c'),
name: 'Bob',
age: 30,
mydatabase> db.mycollection.find({age:{$gt:25}})
Output:
{
_id: ObjectId('6690b18bcece137f5bc4e49c'),
name: 'Bob',
age: 30,
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,