mongod
mongo
db
show dbs
use database_name
show collections
use new_database
use database_to_drop
db.dropDatabase()
db.createCollection("collection_name")
db.collection_name.drop()
db.collection_name.insertOne({
field1: "value1",
field2: "value2"
})
db.collection_name.insertMany([
{ field1: "value1", field2: "value2" },
{ field1: "value3", field2: "value4" }
])
db.collection_name.find()
db.collection_name.find({ field: "value" })
db.collection_name.findOne({ field: "value" })
db.collection_name.find().limit(5)
db.collection_name.find().sort({ field: 1 }) // 1 for ascending, -1 for descending
db.collection_name.countDocuments({ field: "value" })
db.collection_name.find({ field: { $gt: value } }) // greater than
db.collection_name.find({ field: { $lt: value } }) // less than
db.collection_name.find({ field: { $gte: value } }) // greater than or equal
db.collection_name.find({ field: { $lte: value } }) // less than or equal
db.collection_name.find({ field: { $ne: value } }) // not equal
db.collection_name.find({ field: { $in: [value1, value2] } }) // in array
db.collection_name.find({ $and: [{ field1: "value1" }, { field2: "value2" }] })
db.collection_name.find({ $or: [{ field1: "value1" }, { field2: "value2" }] })
db.collection_name.find({ field: { $not: { $eq: value } } })
db.collection_name.updateOne(
{ field: "value" },
{ $set: { field_to_update: "new_value" } }
)
db.collection_name.updateMany(
{ field: "value" },
{ $set: { field_to_update: "new_value" } }
)
db.collection_name.replaceOne(
{ field: "value" },
{ new_field1: "new_value1", new_field2: "new_value2" }
)
db.collection_name.deleteOne({ field: "value" })
db.collection_name.deleteMany({ field: "value" })
db.collection_name.aggregate([
{ $match: { field: "value" } },
{ $group: { _id: "$group_field", total: { $sum: "$sum_field" } } },
{ $sort: { total: -1 } }
])
// $match
db.collection_name.aggregate([
{ $match: { status: "active" } }
])
// $group
db.collection_name.aggregate([
{ $group: { _id: "$category", count: { $sum: 1 } } }
])
// $project
db.collection_name.aggregate([
{ $project: { field1: 1, field2: 1, _id: 0 } }
])
// $sort
db.collection_name.aggregate([
{ $sort: { field: 1 } }
])
// $limit
db.collection_name.aggregate([
{ $limit: 5 }
])
// $unwind
db.collection_name.aggregate([
{ $unwind: "$array_field" }
])
db.collection_name.createIndex({ field: 1 }) // 1 for ascending, -1 for descending
db.collection_name.createIndex({ field: 1 }, { unique: true })
db.collection_name.getIndexes()
db.collection_name.dropIndex("index_name")
db.createUser({
user: "username",
pwd: "password",
roles: [{ role: "readWrite", db: "database_name" }]
})
db.grantRolesToUser(
"username",
[{ role: "readWrite", db: "database_name" }]
)
db.getUsers()
db.dropUser("username")
mongodump --db database_name --out backup_directory
mongorestore --db database_name backup_directory
In the MongoDB configuration file (mongod.conf):
replication:
replSetName: "rs0"
rs.initiate()
rs.add("hostname:port")
rs.status()
sh.enableSharding("database_name")
sh.shardCollection("database_name.collection_name", { "shard_key": 1 })
sh.addShard("hostname:port")
sh.status()
db.collection_name.find({ field: "value" }).explain("executionStats")
db.collection_name.stats()
db.stats()
db.setProfilingLevel(1) // 0: off, 1: slow queries, 2: all queries
db.system.profile.find().pretty()
2024 © All rights reserved - buraxta.com