MongoDB数据库管理实战

                                              作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.库的常用操作

1.查看数据库

	1.全称
test> show databases;
admin         40.00 KiB
config       108.00 KiB
local         40.00 KiB
test          72.00 KiB
yinzhengjie   40.00 KiB
test> 

	2.简写形式
test> show dbs
admin         40.00 KiB
config       108.00 KiB
local         40.00 KiB
test          72.00 KiB
yinzhengjie   40.00 KiB
test> 

2.查看当前所在数据库

test> db  
test
test>

3. 切换数据库

	1.切换到指定数据库
test> use yinzhengjie
switched to db yinzhengjie
yinzhengjie> 
yinzhengjie> db
yinzhengjie
yinzhengjie> 


	2.可以切换到不存在的数据库【如果库内没有数据,则不使用时会自动删除该"临时库"】
yinzhengjie> use jasonyin
switched to db jasonyin
jasonyin> show dbs
admin         40.00 KiB
config       108.00 KiB
local         40.00 KiB
test          72.00 KiB
yinzhengjie   40.00 KiB
jasonyin> db
jasonyin
jasonyin> 

4.删除当前所在的库

jasonyin> use yinzhengjie
switched to db yinzhengjie
yinzhengjie> show dbs
admin         40.00 KiB
config       108.00 KiB
local         40.00 KiB
test          72.00 KiB
yinzhengjie   40.00 KiB
yinzhengjie> db.dropDatabase()
{ ok: 1, dropped: 'yinzhengjie' }
yinzhengjie> show dbs
admin    40.00 KiB
config  108.00 KiB
local    40.00 KiB
test     72.00 KiB
yinzhengjie> 

二.集合(表)常用操作

1.查看集合(表)

yinzhengjie> show tables

yinzhengjie> show collections

yinzhengjie> 


温馨提示:
	"show tables"和"show collections"都是用来查看当前库中表或者集合的列表。

2.创建集合(表)

	1.直接创建表
yinzhengjie> db.createCollection('hbase')
{ ok: 1 }
yinzhengjie> show tables
hbase
yinzhengjie> show collections
hbase
yinzhengjie> 


	2.当插入一个文档时,一个集合就会自动创建
yinzhengjie> db.bigdata.insertOne({name: "Hadoop",content: "HDFS|YARN|MAPREDUCE"})
{
  acknowledged: true,
  insertedId: ObjectId('67e2d4111fd4b481d7013382')
}
yinzhengjie> db.bigdata_stream.insertMany([{name: "Spark",content: "On yarn"},{name: "Flink", content: "On k8s"}])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId('67e2d41f1fd4b481d7013383'),
    '1': ObjectId('67e2d41f1fd4b481d7013384')
  }
}
yinzhengjie> show collections
bigdata
bigdata_stream
hbase
yinzhengjie> show tables
bigdata
bigdata_stream
hbase
yinzhengjie> 

	3.也可以支持用循环语句批量写入数据
yinzhengjie> for(i=0;i<10000;i++){db.teacher.insertOne({tid: i, name: "杰哥讲运维"+i,age: 18,date: new Date()})}
{
  acknowledged: true,
  insertedId: ObjectId('67e2d7bf1fd4b481d7044d2d')
}
yinzhengjie> show tables
bigdata
bigdata_stream
hbase
teacher
yinzhengjie> 

3.查看集合(表)信息

	1.查看teacher集合信息
yinzhengjie> db.teacher.stats()
{
  ok: 1,
  capped: false,
  ...	
  sharded: false,
  size: 838890,
  count: 10000,
  numOrphanDocs: 0,
  storageSize: 229376,
  totalIndexSize: 139264,
  totalSize: 368640,
  indexSizes: { _id_: 139264 },
  avgObjSize: 83,
  ns: 'yinzhengjie.teacher',
  nindexes: 1,
  scaleFactor: 1
}
yinzhengjie> 


	2.只查看行数统计
yinzhengjie> db.teacher.countDocuments()
10000
yinzhengjie> 


	3.查询集合存储信息,包括集合中索引和数据压缩存储后的大小
yinzhengjie> db.teacher.totalSize()
368640
yinzhengjie> 

4.修改表名

yinzhengjie> show tables
bigdata
bigdata_stream
hbase
teacher
yinzhengjie>
yinzhengjie> db.bigdata.renameCollection("xixi")
{ ok: 1 }
yinzhengjie> show tables
bigdata_stream
hbase
teacher
xixi
yinzhengjie> 

5.删除表

yinzhengjie> show tables
bigdata_stream
hbase
teacher
xixi
yinzhengjie> db.xixi.drop()
true
yinzhengjie> show tables
bigdata_stream
hbase
teacher
yinzhengjie> 

三.文档(记录)常用操作

1.全表查询(默认只显示前20行,使用it关键字翻页)

yinzhengjie> db.teacher.find()  # 查看表中的数据
[
  {
    _id: ObjectId('67e2d7b31fd4b481d704261e'),
    tid: 0,
    name: '杰哥讲运维0',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.735Z')
  },
  ...,
  {
    _id: ObjectId('67e2d7b31fd4b481d7042631'),
    tid: 19,
    name: '杰哥讲运维19',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.777Z')
  }
]
Type "it" for more
yinzhengjie> it  # 使用it命令可以实现翻页的效果
[
  {
    _id: ObjectId('67e2d7b31fd4b481d7042632'),
    tid: 20,
    name: '杰哥讲运维20',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.780Z')
  },
  ...,
  {
    _id: ObjectId('67e2d7b31fd4b481d7042645'),
    tid: 39,
    name: '杰哥讲运维39',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.826Z')
  }
]
Type "it" for more
yinzhengjie> 

2.只显示前5个

yinzhengjie> db.teacher.find().limit(5)
[
  {
    _id: ObjectId('67e2d7b31fd4b481d704261e'),
    tid: 0,
    name: '杰哥讲运维0',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.735Z')
  },
  {
    _id: ObjectId('67e2d7b31fd4b481d704261f'),
    tid: 1,
    name: '杰哥讲运维1',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.743Z')
  },
  {
    _id: ObjectId('67e2d7b31fd4b481d7042620'),
    tid: 2,
    name: '杰哥讲运维2',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.745Z')
  },
  {
    _id: ObjectId('67e2d7b31fd4b481d7042621'),
    tid: 3,
    name: '杰哥讲运维3',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.746Z')
  },
  {
    _id: ObjectId('67e2d7b31fd4b481d7042622'),
    tid: 4,
    name: '杰哥讲运维4',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.747Z')
  }
]
yinzhengjie> 

3.跳过前面10个显示后面的文档

yinzhengjie> db.teacher.find().skip(10)
[
  {
    _id: ObjectId('67e2d7b31fd4b481d7042628'),
    tid: 10,
    name: '杰哥讲运维10',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.757Z')
  },
  ...,
  {
    _id: ObjectId('67e2d7b31fd4b481d704263b'),
    tid: 29,
    name: '杰哥讲运维29',
    age: 18,
    date: ISODate('2025-03-25T16:20:03.804Z')
  }
]
Type "it" for more
yinzhengjie> 

4.按条件查询

	1.查询tid为5200的文档数据
yinzhengjie> db.teacher.find({tid: 5200})
[
  {
    _id: ObjectId('67e2d7ba1fd4b481d7043a6e'),
    tid: 5200,
    name: '杰哥讲运维5200',
    age: 18,
    date: ISODate('2025-03-25T16:20:10.183Z')
  }
]
yinzhengjie> 


	2.查询tid大于9995并倒序输出
yinzhengjie> db.teacher.find({tid: {$gte: 9995}}).sort({tid: -1})
[
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2d'),
    tid: 9999,
    name: '杰哥讲运维9999',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.357Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2c'),
    tid: 9998,
    name: '杰哥讲运维9998',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.356Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2b'),
    tid: 9997,
    name: '杰哥讲运维9997',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.355Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2a'),
    tid: 9996,
    name: '杰哥讲运维9996',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.355Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d29'),
    tid: 9995,
    name: '杰哥讲运维9995',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.354Z')
  }
]


	3.查询tid大于9995并正序输出
yinzhengjie> db.teacher.find({tid: {$gte: 9995}}).sort({tid: 1})
[
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d29'),
    tid: 9995,
    name: '杰哥讲运维9995',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.354Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2a'),
    tid: 9996,
    name: '杰哥讲运维9996',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.355Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2b'),
    tid: 9997,
    name: '杰哥讲运维9997',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.355Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2c'),
    tid: 9998,
    name: '杰哥讲运维9998',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.356Z')
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2d'),
    tid: 9999,
    name: '杰哥讲运维9999',
    age: 18,
    date: ISODate('2025-03-25T16:20:15.357Z')
  }
]
yinzhengjie> 
 

5.查看集合的指定字段

yinzhengjie> db.teacher.find({ tid: { $gte: 9995 }},{name: 1,tid: 1}).sort({ tid: -1 }).limit(3)
[
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2d'),
    tid: 9999,
    name: '杰哥讲运维9999'
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2c'),
    tid: 9998,
    name: '杰哥讲运维9998'
  },
  {
    _id: ObjectId('67e2d7bf1fd4b481d7044d2b'),
    tid: 9997,
    name: '杰哥讲运维9997'
  }
]
yinzhengjie> 

6.修改文档的某个字段

yinzhengjie> db.teacher.find({tid: 7777})
[
  {
    _id: ObjectId('67e2d7bd1fd4b481d704447f'),
    tid: 7777,
    name: '杰哥讲运维7777',
    age: 18,
    date: ISODate('2025-03-25T16:20:13.048Z')
  }
]
yinzhengjie> db.teacher.updateOne({tid: 7777},{$set: {age: 32}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
yinzhengjie> db.teacher.find({tid: 7777})
[
  {
    _id: ObjectId('67e2d7bd1fd4b481d704447f'),
    tid: 7777,
    name: '杰哥讲运维7777',
    age: 32,
    date: ISODate('2025-03-25T16:20:13.048Z')
  }
]
yinzhengjie> 

7.删除文档

	1.删除指定的文档
yinzhengjie> db.teacher.find({tid: 8888})  # 删除前可以查看到数据
[
  {
    _id: ObjectId('67e2d7be1fd4b481d70448d6'),
    tid: 8888,
    name: '杰哥讲运维8888',
    age: 18,
    date: ISODate('2025-03-25T16:20:14.206Z')
  }
]
yinzhengjie> db.teacher.deleteOne({tid: 8888})  # 删除指定文档
{ acknowledged: true, deletedCount: 1 }
yinzhengjie> db.teacher.find({tid: 8888})  # 无法查询数据啦~

yinzhengjie> 


	2.移除集合的所有文档
yinzhengjie> db.teacher.count()
9998
yinzhengjie> 
yinzhengjie> db.teacher.remove({})
{ acknowledged: true, deletedCount: 9998 }
yinzhengjie> db.teacher.count()
0
yinzhengjie> 
	
posted @ 2025-03-18 00:49  尹正杰  阅读(169)  评论(0)    收藏  举报