当前位置:   article > 正文

Mongodb集合操作

Mongodb集合操作

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.6

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2024-07-30T10:58:29.512+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2024-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> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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

test> use atguigu
switched to db atguigu
  • 1
  • 2

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> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4、查看数据库列表

atguigu> show dbs
admin    40.00 KiB
atguigu  40.00 KiB
config   96.00 KiB
local    72.00 KiB
  • 1
  • 2
  • 3
  • 4
  • 5

5、查看集合

atguigu> show collections
myCollection
  • 1
  • 2

6、显示创建集合

atguigu> db.createCollection("myCollection2")
{ ok: 1 }
atguigu> show collections
myCollection
myCollection2
  • 1
  • 2
  • 3
  • 4
  • 5

7、删除集合

atguigu> db.myCollection2.drop()
true
atguigu> show collections
myCollection
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/919482
推荐阅读
相关标签
  

闽ICP备14008679号