You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
db.posts.update({ title: 'Post Two' },
{
title: 'Post Two', // since $set: {} is not use, this will delete all the formal fields then replace field currently listed
body: 'New body for post 2',
date: Date()
},
{
upsert: true // it means create a document if does not exist with the following detail or just update record with
// title`: 'Post Two'
})
Update Specific Field
db.posts.update({ title: 'Post Two' },
{
$set: {. // this will update only specific field listed inside $set: {}
body: 'Body for post 2',
category: 'Technology'
}
})
Increment Field ($inc)
db.posts.update({ title: 'Post Two' },
{
// $inc is use to increase a particular field which number specified is added to earlier figure it has
$inc: {
likes: 5
}
})
Rename Field
db.posts.update({ title: 'Post Two' },
{
// $rename is use to rename field
$rename: {
likes: 'views'
}
})
db.posts.createIndex({ title: 'text' }) //createIndex is use to create search value which the db can be queried with e.g text
Text Search
db.posts.find({
// $text is the index created which is tied to title column which will be use to search all title fields of the db & return
// where the title field match inputted value of the $search keyword
$text: {
$search: "\"Post O\""
}
})
Greater & Less Than
db.posts.find({ views: { $gt: 2 } }) // $gt means greater than
db.posts.find({ views: { $gte: 7 } }) // $gt means greater than or equal to
db.posts.find({ views: { $lt: 7 } }) // $lt means less than or equal to
db.posts.find({ views: { $lte: 7 } }) // $lt means less than or equal to