Jul 12, 2024
show dbs
use <database_name>
db.createCollection(<collection_name>)show collections
db.dropDatabase()
db.<collection>.insertOne({ <field>: <value>, ... })
db.students.insertOne({ name: "SpongeBob", age: 30, GPA: 3.2 })db.<collection>.find()
db.<collection>.insertMany([{ <field>: <value>, ... }, ... ])new Date() for current date and time.db.<collection>.find().sort({ <field>: 1 | -1 })
1) or descending (-1) order.db.<collection>.find().limit(<number>)
db.students.find().sort({ GPA: -1 }).limit(1)db.<collection>.find({ <query> }, { <projection> })
{ name: "SpongeBob" }{ GPA: 4.0 }{ _id: false, name: true, GPA: true }_db.<collection>.updateOne({ <filter> }, { $set: { <field>: <value> } })
db.students.updateOne({ name: "SpongeBob" }, { $set: { fullTime: true } })db.<collection>.updateMany({ <filter> }, { $set: { <field>: <value> } })db.<collection>.updateOne({ <filter> }, { $unset: { <field>: "" } })db.<collection>.deleteOne({ <filter> })
db.students.deleteOne({ name: "Larry" })db.<collection>.deleteMany({ <filter> }){ name: "Larry" }{ <field>: { $exists: <boolean> } }{ <field>: { $ne: <value> } }{ <field>: { $lt: <value> } }{ <field>: { $gt: <value> } }{ <field>: { $gte: <min_value>, $lte: <max_value> } }{ <field>: { $in: [ <value1>, <value2> ] } }{ <field>: { $nin: [ <value1>, <value2> ] } }{ $and: [ { <expression1> }, { <expression2> } ] }{ $or: [ { <expression1> }, { <expression2> } ] }{ $nor: [ { <expression1> }, { <expression2> } ] }{ <field>: { $not: { <operator>: <value> } } }db.<collection>.createIndex({ <field>: 1 | -1})
{ name: 1 } (ascending order)db.<collection>.getIndexes()db.<collection>.dropIndex(<index_name>)db.createCollection(<name>, { options })
capped, size, max, autoIndexIddb.<collection>.drop()