码迷,mamicode.com
首页 > 其他好文 > 详细

mongo命令

时间:2019-10-15 20:40:47      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:port   database   find   --   class   返回   print   inf   nstat   

3.2 打开数据库连接池

mongod --dbpath d:\data\db
// 如果报错
./mongod --dbpath d:\data\db

如果还不可用,就用管理员身份去运行

最后还不可用,换电脑或者换系统

如果输出 waiting for connections on port 27017 表明连接池打开成功

3.3 打开命令行的数据库客户端

打开压缩的mongodb文件夹,进入bin目录

shift + 右键 选择打开 命令行窗口

3.4 数据库常用命令

help 查看帮助文档

db.help() 数据库的帮助文档

db.test.help() 当前数据库下test集合的帮助文档

db.test.find().help() 当前数据库下test集合的查询的帮助文档

show dbs 查询当前数据库连接池中的所有的数据库

admin       0.000GB
local       0.000GB

use sh1908 无则创建并且切换,有则切换

switched to db sh1908

db 查看当前是哪一个数据库

sh1908

db.stats() 当前数据库状态

{
    "db" : "sh1908",
    "collections" : 0,
    "views" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "ok" : 1
}

db.version() 查询当前数据库的版本

3.4.2

db.getMongo() 查看当前DB的链接机器地址

connection to 127.0.0.1:27017

db.dropDatabase() 删除数据库

{ "ok" : 1 }

show dbs 如果数据库没有数据,那么不会显示该数据库

4、collection 聚集集合操作

4.1、创建集合

db.createCollection(‘users‘) 创建了用户集合--创建了用户表

{ "ok" : 1 }

show dbs 此时可以观察到有了sh1908的数据库

语法:db.createCollection(‘users‘, {size: 20, capped: true, max: 100})
创建了集合users,创建的集合是固定的(capped: true,必须size配合的参数,如果达到最大值,会自动覆盖最先的数据),最大的容量为20字节(size:20,单位为字节),最多存储100条数据(max:100)

字段 类型 描述
capped 布尔(可选) 如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。当该值为 true 时,必须指定 size 参数。
autoIndexId 布尔(可选) 如为 true,自动在 _id 字段创建索引。默认为 false。
size 数值(可选) 为固定集合指定一个最大值(以字节计)。如果 capped 为 true,也需要指定该字段。
max 数值(可选) 指定固定集合中包含文档的最大数量。

db.createCollection(‘course‘)

{ "ok" : 1 }

4.2 得到指定名称的聚集集合

db.getCollection(‘user‘)

sh1908.user

4.3 得到当前DB的所有的聚集集合

db.getCollectionNames()

[ "course", "users" ]

4.4 显示当前db所有集合的状态

db.printCollectionStats()

5、document文档操作

增删改查

5.1 插入操作 ----- 增

db.course

sh1908.course

db.course.insert({week1: ‘node‘, week2: ‘vue‘, week5: ‘react+混合开发‘, week8:‘微信相关开发‘, week9: ‘复习‘})

WriteResult({ "nInserted" : 1 })

插入单条数据

db.col.insert({})

db.col.insertOne({})

插入多条数据

db.col.insert([ {}, {}, {} ])

db.col.insertMany([ {}, {}, {} ])

了解

插入文档你也可以使用 db.col.save(document) 命令。如果不指定 _id 字段 save() 方法类似于 insert() 方法。如果指定 _id 字段,则会更新该 _id 的数据。

db.users.insertOne({username: ‘吴大勋‘, password: ‘123456‘, sex: 1, age: 18, lesson: 3, city: ‘山西‘})

{
    "acknowledged" : true,
    "insertedId" : ObjectId("5da5690eee7de50b8cbc6ea5")
}

db.users.insertMany([{username: ‘徐石坡‘, password:‘123456‘, sex: 1, age: 25, lesson: 3, city: ‘安徽‘},{username: ‘操鑫‘, password:‘123456‘, sex: 1, age: 40, lesson: 3, city: ‘安徽‘}])

{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5da56a10ee7de50b8cbc6ea6"),
        ObjectId("5da56a10ee7de50b8cbc6ea7")
    ]
}

5.2 简单查询插入的数据 ---- 看一下数据是什么

db.users.find() 查询当前数据库集合下的所有数据

{ "_id" : ObjectId("5da5690eee7de50b8cbc6ea5"), "username" : "吴大勋", "password" : "123456", "sex" : 1, "age" : 18, "lesson" : 3, "city" : "山西" }
{ "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"), "username" : "徐石坡", "password" : "123456", "sex" : 1, "age" : 25, "lesson" : 3, "city" : "安徽" }
{ "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"), "username" : "操鑫", "password" : "123456", "sex" : 1, "age" : 40, "lesson" : 3, "city" : "安徽" }

db.users.find().pretty() 查询数据并且格式化

{
    "_id" : ObjectId("5da5690eee7de50b8cbc6ea5"),
    "username" : "吴大勋",
    "password" : "123456",
    "sex" : 1,
    "age" : 18,
    "lesson" : 3,
    "city" : "山西"
}
{
    "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
    "username" : "徐石坡",
    "password" : "123456",
    "sex" : 1,
    "age" : 25,
    "lesson" : 3,
    "city" : "安徽"
}
{
    "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"),
    "username" : "操鑫",
    "password" : "123456",
    "sex" : 1,
    "age" : 40,
    "lesson" : 3,
    "city" : "安徽"
}

5.3 删除数据

db.users.deleteOne({username: ‘吴大勋‘}) 删除用户名为吴大勋的一条记录

{ "acknowledged" : true, "deletedCount" : 1 }

db.users.find().pretty()

{
        "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 25,
        "lesson" : 3,
        "city" : "安徽"
}
{
        "_id" : ObjectId("5da56a10ee7de50b8cbc6ea7"),
        "username" : "操鑫",
        "password" : "123456",
        "sex" : 1,
        "age" : 40,
        "lesson" : 3,
        "city" : "安徽"
}

db.users.deleteMany({age: 40}) 删除多条年龄为40岁的记录

{ "acknowledged" : true, "deletedCount" : 1 }

db.users.find().pretty()

{
        "_id" : ObjectId("5da56a10ee7de50b8cbc6ea6"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 25,
        "lesson" : 3,
        "city" : "安徽"
}

db.users.deleteMany({}) 删除所有的数据

{ "acknowledged" : true, "deletedCount" : 1 }

db.users.find().pretty()

// 没有返回

小结

  • db.col.deleteOne({key: value}) 删除单条数据
  • db.col.deleteMany({key: value}) 删除多条数据
  • db.col.deleteMany({}) 删除所有的数据

5.4 修改数据

依据5.1步骤插入数据

db.users.find().pretty()

{                        西'})
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 18,
        "lesson" : 3,
        "city" : "山西"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 25,
        "lesson" : 3,
        "city" : "安徽"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
        "username" : "操鑫",
        "password" : "123456",
        "sex" : 1,
        "age" : 40,
        "lesson" : 3,
        "city" : "安徽"
}

db.users.updateOne({username: ‘吴大勋‘}, { $set: {age: 20}})

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

db.users.find().pretty()

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 20,
        "lesson" : 3,
        "city" : "山西"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 25,
        "lesson" : 3,
        "city" : "安徽"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
        "username" : "操鑫",
        "password" : "123456",
        "sex" : 1,
        "age" : 40,
        "lesson" : 3,
        "city" : "安徽"
}

db.users.updateMany({}, { $set: { company: ‘千锋‘}})

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

db.users.find().pretty()

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 20,
        "lesson" : 3,
        "city" : "山西",
        "company" : "千锋"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 25,
        "lesson" : 3,
        "city" : "安徽",
        "company" : "千锋"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
        "username" : "操鑫",
        "password" : "123456",
        "sex" : 1,
        "age" : 40,
        "lesson" : 3,
        "city" : "安徽",
        "company" : "千锋"
}

db.users.updateMany({}, { $inc: {age: 1}}) // 所有的年龄 +1

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

db.users.find().pretty()

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 21,
        "lesson" : 3,
        "city" : "山西",
        "company" : "千锋"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 26,
        "lesson" : 3,
        "city" : "安徽",
        "company" : "千锋"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
        "username" : "操鑫",
        "password" : "123456",
        "sex" : 1,
        "age" : 41,
        "lesson" : 3,
        "city" : "安徽",
        "company" : "千锋"
}

db.users.updateMany({}, { $inc: {age: -3}})

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

db.users.find().pretty()

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 18,
        "lesson" : 3,
        "city" : "山西",
        "company" : "千锋"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6ea9"),
        "username" : "徐石坡",
        "password" : "123456",
        "sex" : 1,
        "age" : 23,
        "lesson" : 3,
        "city" : "安徽",
        "company" : "千锋"
}
{
        "_id" : ObjectId("5da56dc7ee7de50b8cbc6eaa"),
        "username" : "操鑫",
        "password" : "123456",
        "sex" : 1,
        "age" : 38,
        "lesson" : 3,
        "city" : "安徽",
        "company" : "千锋"
}

小结

  • db.col.updateOne({key:value}, {$set: {key1: value1}})
  • db.col.updateOne({key:value}, {$inc: {key1: 1}})
  • db.col.updateMany({key:value}, {$set: {key1: value1}})
  • db.col.updateMany({key:value}, {$inc: {key1: 1}})
  • db.col.updateMany({}, {$set: {key1: value1}})
  • db.col.updateMany({}, {$inc: {key1: 1}})

5.5 查询数据

db.users.find().pretty()

db.users.find({},{}).pretty() // 同上,第一个{}代表查询的条件,第二个代表显示的字段

db.users.find({username:‘吴大勋‘}).pretty() // 查询用户名为吴大勋的记录

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 18,
        "lesson" : 3,
        "city" : "山西",
        "company" : "千锋"
}

db.users.find({username:‘吴大勋‘}, {username: 1, age: 1}).pretty()

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "age" : 18
}

db.users.find({username:‘吴大勋‘}, {username: 1, age: 1, _id: 0}).pretty()

{ "username" : "吴大勋", "age" : 18 }

db.users.find({}, {username: 1, age: 1, _id: 0}).pretty()

{ "username" : "吴大勋", "age" : 18 }
{ "username" : "徐石坡", "age" : 23 }
{ "username" : "操鑫", "age" : 38 }

db.users.find({ age: { $gte: 30} }, {_id:0, username: 1}).pretty() // 查询大于等于30岁的数据 大于用 $gt

{ "username" : "操鑫" }

db.users.find({ age: { $gte: 18, $lte: 30} }, {_id:0, username: 1}).pretty()

{ "username" : "吴大勋" }
{ "username" : "徐石坡" }

db.users.find({}, {_id:0, username: 1, age: 1}).sort({age: 1}).pretty() // 年龄的升序

{ "username" : "吴大勋", "age" : 18 }
{ "username" : "徐石坡", "age" : 23 }
{ "username" : "操鑫", "age" : 38 }

db.users.find({}, {_id:0, username: 1, age: 1}).sort({age: -1}).pretty() // 年龄的降序

{ "username" : "操鑫", "age" : 38 }
{ "username" : "徐石坡", "age" : 23 }
{ "username" : "吴大勋", "age" : 18 }

db.users.find({username: /大/}, {}).pretty() // 查询名字中带大的数据---模糊查询

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 18,
        "lesson" : 3,
        "city" : "山西",
        "company" : "千锋"
}

db.users.find({username: /^吴/}, {}).pretty() // 查询名字中带吴且开头的数据---模糊查询

{
        "_id" : ObjectId("5da56dbeee7de50b8cbc6ea8"),
        "username" : "吴大勋",
        "password" : "123456",
        "sex" : 1,
        "age" : 18,
        "lesson" : 3,
        "city" : "山西",
        "company" : "千锋"
}

db.users.distinct(‘city‘) // 查询所有数据的city字段组成数组,并且去重

[ "山西", "安徽" ]

db.users.distinct(‘age‘)

[ 18, 23, 38 ]

db.users.find().count() // 查询的条数

3
  • 想要查看其他的查询方法 db.col.find().help()
find(<predicate>, <projection>) modifiers
    .sort({...})
    .limit(<n>)        ************************** 分页
    .skip(<n>)         ************************** 分页
    .batchSize(<n>) - sets the number of docs to return per getMore
    .collation({...})
    .hint({...})
    .readConcern(<level>)
    .readPref(<mode>, <tagset>)
    .count(<applySkipLimit>) - total # of objects matching query. by default ignores skip,limit
    .size() - total # of objects cursor would return, honors skip,limit
    .explain(<verbosity>) - accepted verbosities are {'queryPlanner', 'executionStats', 'allPlansExecution'}
    .min({...})
    .max({...})
    .maxScan(<n>)
    .maxTimeMS(<n>)
    .comment(<comment>)
    .snapshot()
    .tailable(<isAwaitData>)
    .noCursorTimeout()
    .allowPartialResults()
    .returnKey()
    .showRecordId() - adds a $recordId field to each returned object

Cursor methods
    .toArray() - ************************************ 将每一条记录转换成数组
    .forEach(<func>)
    .map(<func>)
    .hasNext()
    .next()
    .close()
    .objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
    .itcount() - iterates through documents and counts them
    .getQueryPlan() - get query plans associated with shape. To get more info on query plans, call getQueryPlan().help().
    .pretty() - pretty print each document, possibly over multiple lines

小结

  • db.col.find()
  • db.col.find().pretty()
  • db.col.find({key: value})
  • db.col.find({}, {_id:0, key1: 1})
  • db.col.find({price: { $gte: 100, $lte: 200}})
  • db.col.find().count()
  • db.col.distinct(‘key‘)
  • db.col.find().toArray() // 转换成数组
  • db.col.find().limit(num) // 只能查询num条数据
  • db.col.find().skip(n) // 从第n条数据开始查询,下标从0开始
  • db.col.find($or: [{age:18, lesson:2}]) // 查询年龄为18 或者 二阶段的数据

mongo命令

标签:port   database   find   --   class   返回   print   inf   nstat   

原文地址:https://www.cnblogs.com/hy96/p/11680145.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!