欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > Mongodb集合操作

Mongodb集合操作

2025/3/14 20:49:30 来源:https://blog.csdn.net/m0_65152767/article/details/140815410  浏览:    关键词:Mongodb集合操作

文章目录

  • 1、进入容器
  • 2、如果数据库不存在,则创建数据库,否则切换到指定数据库
  • 3、在 MongoDB 中,创建集合不是必须操作。当你插入一些文档时,MongoDB 会自动创建集合。
  • 4、查看数据库列表
  • 5、查看集合
  • 6、显示创建集合
  • 7、删除集合

1、进入容器

[root@localhost ~]# docker exec -it mongodb mongosh
Current Mongosh Log ID: 66a99c147066bd635ba48ced
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.10.6
Using MongoDB:          7.0.0
Using Mongosh:          1.10.6For mongosh info see: https://docs.mongodb.com/mongodb-shell/------The server generated these startup warnings when booting2024-07-30T10:58:29.512+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted2024-07-30T10:58:29.513+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'2024-07-30T10:58:29.513+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'2024-07-30T10:58:29.513+00:00: vm.max_map_count is too low
------test> 

2、如果数据库不存在,则创建数据库,否则切换到指定数据库

test> use atguigu
switched to db atguigu

3、在 MongoDB 中,创建集合不是必须操作。当你插入一些文档时,MongoDB 会自动创建集合。

atguigu> db.myCollection.insert({name:'小明',age:20})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{acknowledged: true,insertedIds: { '0': ObjectId("66a99e507066bd635ba48cef") }
}
atguigu> 

4、查看数据库列表

atguigu> show dbs
admin    40.00 KiB
atguigu  40.00 KiB
config   96.00 KiB
local    72.00 KiB

5、查看集合

atguigu> show collections
myCollection

6、显示创建集合

atguigu> db.createCollection("myCollection2")
{ ok: 1 }
atguigu> show collections
myCollection
myCollection2

7、删除集合

atguigu> db.myCollection2.drop()
true
atguigu> show collections
myCollection

在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词