以下是 MongoDB Shell 常用命令:
数据库操作
-
use <database>
:切换到指定的数据库
例如:use mydatabase -
show dbs
:显示所有可用的数据库
例如:
> show dbs
admin 0.000GB
local 0.000GB
test 0.000GB
-
db.dropDatabase()
:删除当前数据库
例如:db.dropDatabase()
集合操作
-
db.createCollection(<name>, <options>)
:创建一个新的集合例如:db.createCollection(“mycollection”)
-
show collections
:显示当前数据库中的所有集合例如:
> show collections
mycollection
-
db.<collection>.drop()
:删除指定的集合例如:
db.mycollection.drop()
-
db.<collection>.insertOne(<document>)
:在指定集合中插入一个文档例如:
> db.mycollection.insertOne({ name: "John", age: 30 })
{
"acknowledged" : true,
"insertedId" : ObjectId("60f2a0e804d6a22b9f7d2c3c")
}
-
db.<collection>.insertMany(<documents>)
:在指定集合中插入多个文档例如:
> db.mycollection.insertMany([
{ name: "John", age: 30 },
{ name: "Jane", age: 25 }
])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("60f2a1b104d6a22b9f7d2c3d"),
ObjectId("60f2a1b104d6a22b9f7d2c3e")
]
}
-
db.<collection>.find()
:显示指定集合中的所有文档例如:
> db.mycollection.find()
{ "_id" : ObjectId("60f2a0e804d6a22b9f7d2c3c"), "name" : "John", "age" : 30 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3d"), "name" : "John", "age" : 30 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3e"), "name" : "Jane", "age" : 25 }
-
db.<collection>.findOne(<filter>)
:显示指定集合中匹配筛选条件的第一个文档例如:
> db.mycollection.findOne({ name: "John" })
{ "_id" : ObjectId("60f2a0e804d6a22b9f7d2c3c"), "name" : "John", "age" : 30 }
-
db.<collection>.updateOne(<filter>, <update>)
:更新指定集合中匹配筛选条件的第一个文档例如:
> db.mycollection.updateOne({ name: "John" }, { $set: { age: 35 } })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.mycollection.find()
{ "_id" : ObjectId("60f2a0e804d6a22b9f7d2c3c"), "name" : "John", "age" : 35 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3d"), "name" : "John", "age" : 30 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3e"), "name" : "Jane", "age" : 25 }
-
db.<collection>.updateMany(<filter>, <update>)
:更新指定集合中匹配筛选条件的所有文档
例如:
> db.mycollection.updateMany({ name: "John" }, { $set: { age: 40 } })
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
> db.mycollection.find()
{ "_id" : ObjectId("60f2a0e804d6a22b9f7d2c3c"), "name" : "John", "age" :40 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3d"), "name" : "John", "age" :40 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3e"), "name" : "Jane", "age" :25 }
-
db.<collection>.deleteOne(<filter>)
:删除指定集合中匹配筛选条件的第一个文档
例如
> db.mycollection.deleteOne({ name: "Jane" })
{ "acknowledged" : true, "deletedCount" :1 }
> db.mycollection.find()
{ "_id" : ObjectId("60f2a0e804d6a22b9f7d2c3c"), "name" : "John", "age" :40 }
{ "_id" : ObjectId("60f2a1b104d6a22b9f7d2c3d"), "name" : "John", "age" :40 }
-
db.<collection>.deleteMany(<filter>)
:删除指定集合中匹配筛选条件的所有文档
例如
> db.mycollection.deleteMany({ name: "John" })
{ "acknowledged" : true, "deletedCount":2 }
> db.mycollection.find()
-db.<collection>.countDocuments(<filter>)
:返回指定集合中匹配筛选条件的文档数量
例如
> db.mycollection.countDocuments()
-
db.<collection>.aggregate(<pipeline>)
:执行聚合操作来处理指定集合中的文档
例如
> db.mycollection.aggregate([
{ $group: { _id: "$name", totalAge: { $sum: "$age"} } }
])
索引操作
db.<collection>.createIndex(<keys>, <options>)
:为指定集合创建一个索引
例如:
> db.mycollection.createIndex({ name:1 })
db.<collection>.getIndexes()
:返回指定集合中的所有索引
例如:
> db.mycollection.getIndexes()