Handson Demo Mongodb
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' }
}