📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
db.posts.insertMany([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Java Guides',
url: 'https://javaguides.net',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'Java Guides',
url: 'https://javaguides.net',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5e18246f7695f4d696a05985"),
ObjectId("5e18246f7695f4d696a05986")
]
}
The find() Method
Syntax
> db.COLLECTION_NAME.find()
Select All Documents in a Collection
db.posts.find( {} )
SELECT * FROM posts
Example
> db.posts.find( {} );
{ "_id" : ObjectId("5e1840697695f4d696a05987"), "title" : "MongoDB Overview", "description" : "MongoDB is no sql database", "by" : "Java Guides", "url" : "https://javaguides.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
{ "_id" : ObjectId("5e1840697695f4d696a05988"), "title" : "NoSQL Database", "description" : "NoSQL database doesn't have tables", "by" : "Java Guides", "url" : "https://javaguides.net", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 20, "comments" : [ { "user" : "user1", "message" : "My first comment", "dateCreated" : ISODate("2013-12-10T09:35:00Z"), "like" : 0 } ] }
The pretty() Method
Syntax
> db.posts.find().pretty()
Example
> db.posts.find().pretty()
{
"_id" : ObjectId("5e1840697695f4d696a05987"),
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
{
"_id" : ObjectId("5e1840697695f4d696a05988"),
"title" : "NoSQL Database",
"description" : "NoSQL database doesn't have tables",
"by" : "Java Guides",
"url" : "https://javaguides.net",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 20,
"comments" : [
{
"user" : "user1",
"message" : "My first comment",
"dateCreated" : ISODate("2013-12-10T09:35:00Z"),
"like" : 0
}
]
}
AND in MongoDB
Syntax
> db.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
>db.mycol.find({$and:[{"by":"Java Guides"},{"title": "MongoDB Overview"}]}).pretty() {
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Java Guides",
"url": "https://javaguides.net",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
OR in MongoDB
Syntax
> db.posts.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
> db.posts.find({$or:[{"by":"Java Guides"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Java Guides",
"url": "https://javaguides.net",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
Using AND and OR Together
Example
>db.mycol.find({"likes": {$gt:10}, $or: [{"by": "tutorials point"},
{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "Java Guides",
"url": "https://javaguides.net",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
Comments
Post a Comment
Leave Comment