- Check
monosh
Version - Start the Mongo Shell
- Show Current Database
- Show All Databases
- Create Or Switch Database
- Drop Database
- Create Collection
- Show Collections
- Insert Document
- Insert Multiple Documents
- Find All Documents
- Find Documents with Query
- Sort Documents
- Count Documents
- Limit Documents
- Chaining
- Find One Document
- Update Document
- Update Document or Insert if not Found
- Increment Field (
$inc
) - Update Multiple Documents
- Rename Field
- Delete a Document
- Delete Multiple Documents
- Greater & Less Than
- Complex-Filter-Objects
- Check for equality
- Check for inequality
- Check for > & >=
- Check for more than one
- Check if a value is none of many values
- Check that multiple conditions are all true
- Check that one of multiple conditions is true
- Negate the filter inside of $not
- Check if a field exists
Open a connection to your local MongoDB instance. All other commands will be runw ithin this mongosh connection.
mongosh --version
mongosh "YOUR_CONNECTION_STRING" --username YOUR_USER_NAME
Show current database name
db
Show all databases in the current MongoDB instance
show dbs
Creates the database if does not exist else switch to the existing database
use blog
Delete the current database
db.dropDatabase()
db.createCollection('posts')
Show all collections in the current database
show collections
Create a new document inside the specified collection
db.posts.insertOne({
title: 'Post 1',
body: 'Body of post.',
category: 'News',
likes: 1,
tags: ['news', 'events'],
date: Date()
})
Create a new document inside the specified collection
db.posts.insertMany([
{
title: 'Post 2',
body: 'Body of post.',
category: 'Event',
likes: 2,
tags: ['news', 'events'],
date: Date()
},
{
title: 'Post 3',
body: 'Body of post.',
category: 'Tech',
likes: 3,
tags: ['news', 'events'],
date: Date()
},
{
title: 'Post 4',
body: 'Body of post.',
category: 'Event',
likes: 4,
tags: ['news', 'events'],
date: Date()
},
{
title: 'Post 5',
body: 'Body of post.',
category: 'News',
likes: 5,
tags: ['news', 'events'],
date: Date()
}
])
db.posts.find()
Find all documents that match the filter obejct
db.posts.find({ category: 'News' })
Sort the results of a find by the given fields Get all users sorted by name in alphabetical order and then if nay names are the same sort by aga in reverse order
db.posts.find().sort({ title: 1 })
db.posts.find().sort({ title: -1 })
db.posts.find().count()
db.posts.find({ category: 'news' }).count()
db.posts.find().limit(2)
db.posts.find().limit(2).sort({ title: 1 })
db.posts.findOne({ likes: { $gt: 3 } })
db.posts.updateOne({ title: 'Post 1' },
{
$set: {
category: 'Tech'
}
})
db.posts.updateOne({ title: 'Post 6' },
{
$set: {
title: 'Post 6',
body: 'Body of post.',
category: 'News'
}
},
{
upsert: true
})
db.posts.updateOne({ title: 'Post 1' },
{
$inc: {
likes: 2
}
})
db.posts.updateMany({}, {
$inc: {
likes: 1
}
})
db.posts.updateOne({ title: 'Post 2' },
{
$rename: {
likes: 'views'
}
})
db.posts.deleteOne({ title: 'Post 6' })
db.posts.deleteMany({ category: 'Tech' })
db.posts.find({ views: { $gt: 2 } })
db.posts.find({ views: { $gte: 7 } })
db.posts.find({ views: { $lt: 7 } })
db.posts.find({ views: { $lte: 7 } })
Get all users with the name DEVANG
db.users.find({name: {$eq :"DEVANG"}})
Get all the users with a name other than DEVANG
db.users.find((name: {&ne :"DEVANG"}))
Get all uses with an age greater than 12 Get all users with an age greater than or equal to 15
$gt/$gte
db.users.find({age: {$gt:12}})
db.users.find({age: {$gte:15}})
Get all uses with an age less than 12 Get all users with an age less than or equal to 15
$lt/$lte
db.users.find({age: {$lt:12}})
db.users.find({age: {$lte:15}})
Get all users with a name of Swastik or Shubham
$in
db.users.find({age: {$in:["Shubham","Swastik"]}})
Get all users that do not have the name Swastik or Shubham
$nin
db.users.find({ name: { $nin: ["Shubham", "Swastik"] } })
Get all users that have an age of 19 and the name Shubham This is an alternative way to do the same thing. Generally you do not need $and
$and
db.users.find({ $and: [{ age: 19 }, { name: "Swastik" }] })
db.users.find({ age: 19, name: "Shubham" })
Get all users with a name of Swastik or an age of 19
$or
db.users.find({ $or: [{ age: 19 }, { name: "Swastik" }] })
Get all users with a name other than Devang
db.users.find({ name: { $not: { $eq: "Devang" } } })
Get all users that have a name field
db.users.find({ name: { $exists: true } })