- Get all documents in a collection
db.getCollection('books').find({})
reasult:
{
"_id" : ObjectId("5ebda0bf1229b4654e39568b"),
"title" : "First book title",
"description" : "First book description",
"price" : 11.0
}
/* 2 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568c"),
"title" : "Second book title",
"description" : "Second book description",
"price" : 22.0
}
/* 3 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568d"),
"title" : "Third book title",
"description" : "Third book description",
"price" : 33.0
}
- Get all documents in a collection where the price is greater than 15.
db.getCollection('books').find({
price: {$gt: 15}
})
reasult:
/* 1 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568c"),
"title" : "Second book title",
"description" : "Second book description",
"price" : 22.0
}
/* 2 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568d"),
"title" : "Third book title",
"description" : "Third book description",
"price" : 33.0
}
- Update an existing record
db.books.update(
{"_id" : ObjectId("5ebda0bf1229b4654e39568c")},
{$set: {"title": "This title was updated" }}
)
result:
{
"_id" : ObjectId("5ebda0bf1229b4654e39568c"),
"title" : "This title was updated",
"description" : "Second book description",
"price" : 22.0
}
Find those books where the price
is greater than 20 and add a discount of 55
db.books.update(
{"price":{$gt: 20}},
{$set: {"discount": 55}},
{multi: true}
)
result
/* 1 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568b"),
"title" : "First book title",
"description" : "First book description",
"price" : 11.0
}
/* 2 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568c"),
"title" : "This title was updated",
"description" : "Second book description",
"price" : 22.0,
"discount" : 55.0
}
/* 3 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568d"),
"title" : "Third book title",
"description" : "Third book description",
"price" : 33.0,
"discount" : 55.0
}
MongoDB will also create new records if query criteria is met even though there might now be any matching data to update current records it has
db.books.update(
{"_id" : ObjectId("5ebda0bf1229b4654e39567c")},
{$set: {"title": "This title doesn't exists" }},
{upsert:true}
)
result
/* 1 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568b"),
"title" : "First book title",
"description" : "First book description",
"price" : 11.0
}
/* 2 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568c"),
"title" : "This title was updated",
"description" : "Second book description",
"price" : 22.0,
"discount" : 55.0
}
/* 3 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568d"),
"title" : "Third book title",
"description" : "Third book description",
"price" : 33.0,
"discount" : 55.0
}
/* 4 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39567c"),
"title" : "This title doesn't exists"
}
Delete a record in MongoDB by passing in the ObjectId
to the remove method
db.books.remove(
{"_id" : ObjectId("5ebda0bf1229b4654e39567c")}
)
result
/* 1 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568b"),
"title" : "First book title",
"description" : "First book description",
"price" : 11.0
}
/* 2 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568c"),
"title" : "This title was updated",
"description" : "Second book description",
"price" : 22.0,
"discount" : 55.0
}
/* 3 */
{
"_id" : ObjectId("5ebda0bf1229b4654e39568d"),
"title" : "Third book title",
"description" : "Third book description",
"price" : 33.0,
"discount" : 55.0
}