0% found this document useful (0 votes)
17 views11 pages

Handson Demo Mongodb

The document provides a step-by-step guide on how to use MongoDB commands in the Mongosh shell to create a database, collections, and insert documents. It includes examples of inserting single and multiple documents, querying the database, sorting results, updating records, and deleting entries. Additionally, it demonstrates how to perform aggregation operations and check indexes in the database.

Uploaded by

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

Handson Demo Mongodb

The document provides a step-by-step guide on how to use MongoDB commands in the Mongosh shell to create a database, collections, and insert documents. It includes examples of inserting single and multiple documents, querying the database, sorting results, updating records, and deleting entries. Additionally, it demonstrates how to perform aggregation operations and check indexes in the database.

Uploaded by

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

Handson demo MongoDB

In Mongosh shell
1.To create a database
test> use forum
switched to db forum
2.Create collection
forum> db.createCollection("posts")
{ ok: 1 }
forum> show collections
posts
3.Open posts.txt and copy the first content as given below
forum> const doc={
title: "What is the best way to learn JavaScript from the ground up?",
postId: NumberInt(3511),
comments: 10,
shared:true,
tags: [
"JavaScript",
"programming"
],
author: {
name: "Mike Forester",
nickname: "mikef"
}
};

forum> db.posts.insertOne(doc);
--It shows as below
{
acknowledged: true,
insertedId: ObjectId('67c2f2cd31b90cf22bcc8988')
}
4. Open Post.txt and copy the second and third content as given below
db.posts.insertMany([
{
title: "My thoughts about 12-factor App Methodology",
postId: NumberInt(2618),
comments: 0,
shared: false,
tags: [],
author: {
name: "Emily Watson",
nickname: "emily23"
}
},{
title: "Who can suggest best computer coding book for beginners?",
postId: NumberInt(8451),
comments: 2,
shared: false,
tags: [
"programming",
"coding"
],
author: {
name: "Emily Watson",
nickname: "emily23"
}
}
]);
It will show as below
{
acknowledged: true,
insertedIds: {
'0': ObjectId('67c2f41e31b90cf22bcc8989'),
'1': ObjectId('67c2f41e31b90cf22bcc898a')
}
}

5. Open Post.txt and copy the fourth and fifth content as given below
const docArray= [{
title:"I want to start my own business. What I need to do first?",
postId: NumberInt(3015),
comments:25,
shared:true,
tags: [
"business",
"money"
],
author:{
name:"Bob Hutchinson",
nickname:"bob1995"
}
},{
title:"What is the average salary of the junior frontend developer?",
postId:NumberInt(1151),
comments:0,
shared:false,
author:{
name:"Mike Forester",
nickname:"mikef"
}
}];

forum> db.posts.insertMany(docArray);
It will show as below
{
acknowledged: true,
insertedIds: {
'0': ObjectId('67c2f7ad31b90cf22bcc898b'),
'1': ObjectId('67c2f7ad31b90cf22bcc898c')
}
}
6.Execute the below query
forum>db.getCollection('posts').findOne({})
It will show as below
{
_id: ObjectId('67c2f2cd31b90cf22bcc8988'),
title: 'What is the best way to learn JavaScript from the ground up?',
postId: 3511,
comments: 10,
shared: true,
tags: [ 'JavaScript', 'programming' ],
author: { name: 'Mike Forester', nickname: 'mikef' }
}

7.Execute the below query


forum>db.getCollection('posts').findOne({postId:3015})
It will show as below
{
_id: ObjectId('67c2f7ad31b90cf22bcc898b'),
title: 'I want to start my own business. What I need to do first?',
postId: 3015,
comments: 25,
shared: true,
tags: [ 'business', 'money' ],
author: { name: 'Bob Hutchinson', nickname: 'bob1995' }
}
8. .Execute the below query
forum>db.getCollection('posts').findOne({comments: 25})
It will show as below
{
_id: ObjectId('67c2f7ad31b90cf22bcc898b'),
title: 'I want to start my own business. What I need to do first?',
postId: 3015,
comments: 25,
shared: true,
tags: [ 'business', 'money' ],
author: { name: 'Bob Hutchinson', nickname: 'bob1995' }
}

9.Execute the below query


forum> db.getCollection('posts').find({}).limit(2)
It will show as below
[
{
_id: ObjectId('67c2f2cd31b90cf22bcc8988'),
title: 'What is the best way to learn JavaScript from the ground up?',
postId: 3511,
comments: 10,
shared: true,
tags: [ 'JavaScript', 'programming' ],
author: { name: 'Mike Forester', nickname: 'mikef' }
},
{
_id: ObjectId('67c2f41e31b90cf22bcc8989'),
title: 'My thoughts about 12-factor App Methodology',
postId: 2618,
comments: 0,
shared: false,
tags: [],
author: { name: 'Emily Watson', nickname: 'emily23' }
}
]

10.Execute the below query for ascending order


db.getCollection('posts').find({}).sort({comments:1})
It will show as below
[
{
_id: ObjectId('67c2f41e31b90cf22bcc8989'),
title: 'My thoughts about 12-factor App Methodology',
postId: 2618,
comments: 0,
shared: false,
tags: [],
author: { name: 'Emily Watson', nickname: 'emily23' }
},
{
_id: ObjectId('67c2f7ad31b90cf22bcc898c'),
title: 'What is the average salary of the junior frontend developer?',
postId: 1151,
comments: 0,
shared: false,
author: { name: 'Mike Forester', nickname: 'mikef' }
},
{
_id: ObjectId('67c2f41e31b90cf22bcc898a'),
title: 'Who can suggest best computer coding book for beginners?',
postId: 8451,
comments: 2,
shared: false,
tags: [ 'programming', 'coding' ],
author: { name: 'Emily Watson', nickname: 'emily23' }
},
{
_id: ObjectId('67c2f2cd31b90cf22bcc8988'),
title: 'What is the best way to learn JavaScript from the ground up?',
postId: 3511,
comments: 10,
shared: true,
tags: [ 'JavaScript', 'programming' ],
author: { name: 'Mike Forester', nickname: 'mikef' }
},
{
_id: ObjectId('67c2f7ad31b90cf22bcc898b'),
title: 'I want to start my own business. What I need to do first?',
postId: 3015,
comments: 25,
shared: true,
tags: [ 'business', 'money' ],
author: { name: 'Bob Hutchinson', nickname: 'bob1995' }
}
]

11. Execute the below query for descending order


db.getCollection('posts').find({}).sort({comments:-1})
12.Execute the below quert to updateOne record
forum>db.posts.updateOne(
{postid:2618},
{$set:{shared:true}}
)
It will show as below
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}

12.Execute below three queries


forum> db.getCollection('posts').insertOne({postId:NumberInt(1111)})
forum> db.getCollection('posts').insertOne({postId:NumberInt(2222)})
forum> db.getCollection('posts').insertOne({postId:NumberInt(3333)})
13.Execute the below query
forum> db.getCollection('posts').find({})
14.Execute the below query
db.getCollection('posts').deleteOne({postId:1111})
15.Execute the below query
db.getCollection('posts').deleteMany({title:{$exists:false}})
16.Execute the below query
db.posts.aggregate([
{$group:{_id:"$author.name"}}
])
17.Execute the below query
db.posts.aggregate([
{$group:{_id:"$author.nickname"}}
])
18.Execute the below query
db.posts.aggregate([
{$group:{_id:"$author.nickname"}}
])
19.Execute the below query
db.posts.getIndexes()

You might also like