In the terminal type mongosh
to start cli client
$ mongosh
This will give the following output:
Current Mongosh Log ID: 66b5d45f5650b1dc223f83cc
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.3.1
Using MongoDB: 7.0.12
Using Mongosh: 1.3.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting:
2024-08-09T12:29:26.815+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
------
Type cls
to clear the terminal.
Type exit
to exit mongosh.
To display all databases, enter show dbs
test> show dbs
admin 41 kB
config 73.7 kB
local 73.7 kB
Type use <database-name>
to select a particular database. If the database does not exist, it is created.
For example, use admin
will select the admin database.
test> use admin
switched to db admin
Typing use school
will create a new database school
which will then be selected.
test> use students
switched to db students
Currently the show dbs
command will not show the school
database as it does not contain any data.
Note
Any newly created database will not be listed in show dbs
, as it's empty. To change this, collections will have to be added to it.
db.dropDatabase()
will delete the selected database.
For example with the school
database selected,
db.dropDatabase()
will drop the school
database.
school> db.dropDatabase()
{ ok: 1, dropped: 'school' }
{ ok: 1, dropped: 'school' }
means deletion is successful.
To go back to the previous state, type
school> use school
db.createCollection("students")
Type db.createCollection("<collection-name>")
to create a new collection.
For example with the school
database selected, typing db.createCollection("students")
will create a new collection students
.
school> db.createCollection("students")
{ ok: 1 }
{ ok: 1 }
means collection creation is successful.
Now show dbs
will display the school
database.
show collections
will display all collections within the selected database:
school> show collections
students
To rename any collecection simply enter:
db.originalCollectionName.renameCollection('newCollectionName')
db.collection.drop()
school> db.students.insertOne({name: "Spongebob", age: 30, gpa: 3.2})
{
acknowledged: true,
insertedId: ObjectId("66afc2c13aaa44348d3380e4")
}
db.students.find()
will display all objects.
school> db.students.find().limit(1)
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
}
]
The insertMany()
function can import many objects at once. Just make an Array containing all the elements and pass it to the function
school> db.students.insertMany([{name:"Patrick", age:38, gpa:1.5}, {name:"Sandy", age:27, gpa:4.0}, {name: "Gary", age:18, gpa:2.5}])`
A successful insertion will give the following message:
{
acknowledged: true,
insertedIds: {
'0': ObjectId("66afd3763aaa44348d3380e5"),
'1': ObjectId("66afd3763aaa44348d3380e6"),
'2': ObjectId("66afd3763aaa44348d3380e7")
}
}
Now db.students.find()
will give:
school> db.students.find()
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5
}
]
There are many supported data types including Strings, Boolean, Date, null, Array, Object and various other types for numbers. For the more info, click here
school> db.students.insertOne({
name:"Larry",
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: new Date(),
graduationDate: null,
courses: ["Biology", "Chemistry", "Calculus"],
address: { street:"123 Fake St.",
city: "Bikini Bottom",
zip: 12345
}
})
The Date()
constructor by default, will return the current date and time. But you can make a Date
instance with specific Date and Time by passing it as a string:
Date("2023-01-02T00:00:00");
Upon successful insertion:
{
acknowledged: true,
insertedId: ObjectId("66afe0293aaa44348d3380e8")
}
Sorting students in ascending order by name
:
school> db.students.find().sort({name:1})
Sorting students in descending order by name
:
school> db.students.find().sort({name:-1})
Sorting students in ascending order by gpa
:
school> db.students.find().sort({gpa:1})
Sorting students in descending order by gpa
:
school> db.students.find().sort({gpa:-1})
The limit()
function can limit the number of documents/objects returned
Th below statement, returns only one document
school> db.students.find().limit(1)
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
}
]
By default, objects in a collection are sorted by their ObjectID
values in ascending order
The first elements in this sequence is objects is returned, when using limit()
To return three documents:
school> db.students.find().limit(3)
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
}
]
To find student with the highest GPA:
school> db.students.find().sort({gpa:-1}).limit(1)
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
}
]
You can use the find method to search for documents that match a specified query. It's parameters are as follows:
db.collectionName.find({query}, {projection})
To find objects where the fields match specified values:
db.collectionName.find({fieldKey: "fieldValue"})
For example, to find all entries where name
is assigned "Spongebob"
:
school> db.students.find({name: "Spongebob"})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2
}
]
To find all entries where gpa
is assigned 4.0
:
school> db.students.find({gpa: 4.0})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4
}
]
To find all entries where fullTime
is assigned false
:
school> db.students.find({fullTime: false})
[
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
To find all entries where fullTime
is assigned true
:
school> db.students.find({gpa:40, fullTime:true})
The above query does not return anything, since it does not match any entries.
To return only the specified fields:
db.collectionName.find({}, {requiredFieldKey: true})
OR
db.collectionName.find({}, {requiredFieldKey: 1})
To display only the name
fields of students:
db.students.find({}, {name: true})
OR
db.students.find({}, {name: 1})
Example:
school> db.students.find({}, {name: true})
[
{ _id: ObjectId("66afc2c13aaa44348d3380e4"), name: 'Spongebob' },
{ _id: ObjectId("66afd3763aaa44348d3380e5"), name: 'Patrick' },
{ _id: ObjectId("66afd3763aaa44348d3380e6"), name: 'Sandy' },
{ _id: ObjectId("66afd3763aaa44348d3380e7"), name: 'Gary' },
{ _id: ObjectId("66afe0293aaa44348d3380e8"), name: 'Larry' }
]
This would return the _id
and name
fields. Although it's not specified, the _id
key and values are also returned by default.
To exclude a given field:
db.collectionName.find({}, {excludedFieldKey: false})
For example, to return only the name
key and exclude the _id
key:
school> db.students.find({}, {_id:false, name: true})
[
{ name: 'Spongebob' },
{ name: 'Patrick' },
{ name: 'Sandy' },
{ name: 'Gary' },
{ name: 'Larry' }
]
To return the name
and gpa
keys and exclude the _id
key:
school> db.students.find({}, {_id:false, name:true, gpa:true})
[
{ name: 'Spongebob', gpa: 3.2 },
{ name: 'Patrick', gpa: 1.5 },
{ name: 'Sandy', gpa: 4 },
{ name: 'Gary', gpa: 2.5 },
{ name: 'Larry', gpa: 2.8 }
]
db.students.updateOne(filter, update)
school> db.students.updateOne({name:"Spongebob"},{$set:{fullTime:true}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
school> db.students.find({name:"Spongebob"})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: true
}
]
school> db.students.updateOne({_id: ObjectId("66afc2c13aaa44348d3380e4")}, {$set:{fullTime:false}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
school> db.students.find({_id: ObjectId("66afc2c13aaa44348d3380e4")})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
}
]
Removes fields
school> db.students.updateOne({_id: ObjectId("66afc2c13aaa44348d3380e4")}, {$unset:{fullTime:""}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
school> db.students.find({_id: ObjectId("66afc2c13aaa44348d3380e4")})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2
}
All entries will have fullTime added with valure false
school> db.students.updateMany({}, {$set:{fullTime:false}})
{
acknowledged: true,
insertedId: null,
matchedCount: 5,
modifiedCount: 4,
upsertedCount: 0
}
school> db.students.find()
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: false
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
Removes the fullTime property whare the name filed has a value of Gary
db.students.updateOne({name:"Gary"}, {$unset:{fullTime:""}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
school> db.students.find({name:"Gary"})
[
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5
}
]
Removes the fullTime property where the name field has a value of Sandy
school> db.students.updateOne({name:"Sandy"}, {$unset:{fullTime:""}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
school> db.students.find({name:"Sandy"})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4
}
]
Both Sandy and Gary have their fullTime fields removed.
school> db.students.find()
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
Selects all entries which do not have the fullTime property.
Adds the fulTime proerties to those entries.
Sets those vales of FullTime in those entries to true.
db.students.updateMany({fullTime:{$exists:false}}, {$set:{fullTime:true}})
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
Gary and Sandy now have fullTime set to true
school> db.students.find()
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
db.students.deleteOne({name:"Larry"})
{ acknowledged: true, deletedCount: 1 }
school> db.students.find({name:"Larry"})
school>
school> db.students.find()
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
db.students.deleteMany({fullTime:false})
{ acknowledged: true, deletedCount: 2 }
school> db.students.find()
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
school> db.students.deleteMany({registerDate:{$exists:false}})
{ acknowledged: true, deletedCount: 2 }
school> db.students.find()
school>
Reinsert all elements with
db.students.insertMany([{_id:ObjectId("66afc2c13aaa44348d3380e4"),name:'Spongebob',age:30,gpa:3.2,fullTime:false},{_id:ObjectId("66afd3763aaa44348d3380e5"),name:'Patrick',age:38,gpa:1.5,fullTime:false},{_id:ObjectId("66afd3763aaa44348d3380e6"),name:'Sandy',age:27,gpa:4,fullTime:true},{_id:ObjectId("66afd3763aaa44348d3380e7"),name:'Gary',age:18,gpa:2.5,fullTime:true},{_id:ObjectId("66afe0293aaa44348d3380e8"),name:'Larry',age:32,gpa:2.8,fullTime:false,registerDate:ISODate("2024-08-04T20:10:17.770Z"),graduationDate:null,courses:['Biology','Chemistry','Calculus'],address:{street:'123FakeSt.',city:'BikiniBottom',zip:12345}}])
Returns all entries except entries where name is Spongebob
school> db.students.find({name:{$ne:"Spongebob"}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
school> db.students.find({age:{$lt:20}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
school> db.students.find({age:{$lte:27}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
school> db.students.find({age:{$gt:27}})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
school> db.students.find({age:{$gte:27}})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
school> db.students.find({gpa:{$gte:3, $lte:4}})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
}
]
For multiple matches, will return all matching elements.
school> db.students.find({name:{$in:["Spongebob", "Patrick", "Sandy"]}})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
}
]
Opposite of in Operator. Returns non matching items.
school> db.students.find({name:{$nin:["Spongebob", "Patrick", "Sandy"]}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
school> db.students.find({$and: [{fullTime:true}, {age: {$lte: 22}}]})
[
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
school> db.students.find({$or: [{fullTime:true}, {age: {$lte: 22}}]})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
school> db.students.find({$nor: [{fullTime:true}, {age: {$lte: 22}}]})
[
{
_id: ObjectId("66afc2c13aaa44348d3380e4"),
name: 'Spongebob',
age: 30,
gpa: 3.2,
fullTime: false
},
{
_id: ObjectId("66afd3763aaa44348d3380e5"),
name: 'Patrick',
age: 38,
gpa: 1.5,
fullTime: false
},
{
_id: ObjectId("66afe0293aaa44348d3380e8"),
name: 'Larry',
age: 32,
gpa: 2.8,
fullTime: false,
registerDate: ISODate("2024-08-04T20:10:17.770Z"),
graduationDate: null,
courses: [ 'Biology', 'Chemistry', 'Calculus' ],
address: { street: '123 Fake St.', city: 'Bikini Bottom', zip: 12345 }
}
]
Lets make some alternations
db.students.find({age:{$lt:30}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: 18,
gpa: 2.5,
fullTime: true
}
]
Setting Gary's age to null
school> db.students.updateOne({name:"Gary"}, {$set:{age:null}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Now
school> db.students.find({age:{$lt:30}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
}
]
Ruturns all entries where age is not greater than or equal to 30
school> db.students.find({age:{$not:{$gte:30}}})
[
{
_id: ObjectId("66afd3763aaa44348d3380e6"),
name: 'Sandy',
age: 27,
gpa: 4,
fullTime: true
},
{
_id: ObjectId("66afd3763aaa44348d3380e7"),
name: 'Gary',
age: null,
gpa: 2.5,
fullTime: true
}
]