๐ INSERT โ insertOne() & DELETE โ deleteOne()
MongoDB provides two methods for inserting documents into a collection:
.insertOne(): Inserts a single document.insertMany(): Inserts multiple documents at once
And just like insert, MongoDB provides:
.deleteOne(): Deletes the first matching document..deleteMany(): Deletes all matching documents.
Example: Insert a new reviewโ
db.reviews.insertOne({
text: "Best book I have ever read.",
rating: 5,
name: "Jack",
bookId: "0786222727",
});
Equivalent SQL queryโ
INSERT INTO reviews (text, rating, name, bookId)
VALUES ('Best book I have ever read.', 5, 'Jack', '0786222727');
Example: Insert multiple reviewsโ
db.reviews.insertMany([
{
text: "Amazing plot.",
rating: 5,
name: "John",
bookId: "0786222727",
},
{
text: "Over-hyped, tbh I feel it's very boring!",
rating: 2,
name: "Nick",
bookId: "0786222727",
},
]);
Example: Delete one reviewโ
db.reviews.deleteOne({
bookId: "0786222727",
});
Equivalent SQL queryโ
DELETE FROM reviews WHERE bookId = '0786222727';
Challengeโ
๐ 1. Insert 4 more reviews for bookId "0786222727".โ
Answer
- JavaScript
- mongosh
- C#
- Python
- Java
const reviews = db.collection("reviews");
await reviews.insertMany([
{
text: "Thrilling end.",
rating: 4,
name: "Mark",
bookId: "0786222727",
},
{
text: "Must read!",
rating: 5,
name: "Raj",
bookId: "0786222727",
},
{
text: "Very expensive",
rating: 3,
name: "Yun",
bookId: "0786222727",
},
{
text: "Extremely satisfied with the storyline!",
rating: 5,
name: "Lisa",
bookId: "0786222727",
}
]);
db.reviews.insertMany([
{
text: "Thrilling end.",
rating: 4,
name: "Mark",
bookId: "0786222727",
},
{
text: "Must read!",
rating: 5,
name: "Raj",
bookId: "0786222727",
},
{
text: "Very expensive",
rating: 3,
name: "Yun",
bookId: "0786222727",
},
{
text: "Extremely satisfied with the storyline!",
rating: 5,
name: "Lisa",
bookId: "0786222727",
}
]);
var newReviews = new[]
{
new Review { Text = "Thrilling end.", Rating = 4, Name = "Mark", BookId = "0786222727" },
new Review { Text = "Must read!", Rating = 5, Name = "Raj", BookId = "0786222727" },
new Review { Text = "Very expensive", Rating = 3, Name = "Yun", BookId = "0786222727" },
new Review { Text = "Extremely satisfied with the storyline!", Rating = 5, Name = "Lisa", BookId = "0786222727" }
};
reviewsCollection.InsertMany(newReviews);
reviews = db["reviews"]
reviews.insert_many([
{
"text": "Thrilling end.",
"rating": 4,
"name": "Mark",
"bookId": "0786222727",
},
{
"text": "Must read!",
"rating": 5,
"name": "Raj",
"bookId": "0786222727",
},
{
"text": "Very expensive",
"rating": 3,
"name": "Yun",
"bookId": "0786222727",
},
{
"text": "Extremely satisfied with the storyline!",
"rating": 5,
"name": "Lisa",
"bookId": "0786222727",
}
])
var reviews = library.getCollection("reviews");
reviews.insertMany(List.of(
new Document("text", "Thrilling end.")
.append("rating", 4)
.append("name", "Mark")
.append("bookId", "0786222727"),
new Document("text", "Very expensive")
.append("rating", 3)
.append("name", "Yun")
.append("bookId", "0786222727"),
new Document("text", "Must read!.")
.append("rating", 6)
.append("name", "Raj")
.append("bookId", "0786222727"),
new Document("text", "Extremely satisfied with the storyline!")
.append("rating", 5)
.append("name", "Lisa")
.append("bookId", "0786222727")));
๐ 2. Delete all the reviews for bookId "0786222727" through a single command.โ
Answer
- JavaScript
- mongosh
- C#
- Python
- Java
const reviews = db.collection("reviews");
await reviews.deleteMany({"bookId": "0786222727"})
db.reviews.deleteMany({"bookId": "0786222727"})
IMongoCollection<Review> reviewsCollection = db.GetCollection<Review>("reviews");
var deletionResult = reviewsCollection.DeleteMany(r => r.BookId == "0786222727");
Console.WriteLine($"{deletionResult.DeletedCount} review(s) deleted.");
reviews = db["reviews"]
reviews.delete_many({"bookId": "0786222727"})
import static com.mongodb.client.model.Filters.eq;
import org.bson.conversions.Bson;
MongoCollection<Document> reviews = library.getCollection("reviews");
Bson filter = eq("bookId", "0786222727");
DeleteResult result = reviews.deleteMany(filter);
System.out.println(result.getDeletedCount() + " reviews deleted.");