MongoDBShell 常用命令及示例

以下是 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值