数据库 MongoDB(一):CentOS7离线安装MongoDB单机版与简单使用 - 131+08:00: vm
to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
2023-10-25T16:17:10.130+08:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
2023-10-25T16:17:10.130+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2023-10-25T16:17:10.131+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
2023-10-25T16:17:10.131+08:00: vm.max_map_count is too low
------
Deprecation warnings:
- Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
test>
4.1、操作数据库
4.1.1、显示当前数据库
显示当前数据库:
test>db
test
这将显示当前选定的数据库。
4.1.2、显示所有数据库
显示所有数据库:
test>show dbs
admin 40.00 KiB
config 72.00 KiB 拼多多帮助力入口
local 72.00 KiB
test 72.00 KiB
test>
这将显示服务器上的所有数据库。
4.1.3、切换到其他数据库
注意下面的命令在切换数据库时,如果数据库不存在,则会创建数据库,否则切换到指定数据库:
use
将替换为您要切换到的数据库名称。如果该数据库不存在,MongoDB 将在您首次向其写入数据时创建该数据库。 MongoDB中数据库的概念与关系型数据库中数据库的概念基本一致。
4.2、对集合的操作
4.2.1、显示数据库中所有集合
集合存在于MongoDB的单个数据库中,它等效于关系型数据库中的表。使用下面三种方式都可以查询数据库中的所有集合:
test>show collections
liCollection
test>
test>show tables
liCollection
test>
test>db.getCollectionNames()
[ 'liCollection' ]
test>
4.2.2、创建集合
使用use命令切换到要创建集合的数据库,使用db.createCollection()命令创建集合,要创建的集合的名称作为参数,例如如下命令创建了一个名为myCollection的集合:
test>db.createCollection("myCollection")
{ ok: 1 }
test>
注意:如果在向集合中插入文档时,假如集合并不存在,那么将自动创建该集合,下面的命令向myCollectionOne集合插入一条数据,但是集合并未事先创建,执行插入操作时集合myCollectionOne将会被自动创建:
test>db.myCollectionOne.insertOne({name:"Jhone", age:23})
{
acknowledged: true,
insertedId: ObjectId("654ed4c0fba0f2e5ddf52464")
}
test>
4.2.3、删除集合
删除集合,可以使用db.yourCollectionName.drop()命令,下面是删除集合的示例:
test>db.liCollection.drop()
true
test>
如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。请注意,集合删除是一个不可逆的操作,一旦删除,其中的数据将无法恢复。
4.2.3、集合改名
MongoDB中,可以使用 db.collection.renameCollection() 命令来给集合改名:
test>db.myCollectionOne.renameCollection("newMyCollection")
{ ok: 1 }
test>
4.3、对文档的操作
MongoDB中文档一词源于document,文档的数据结构和JSON基本一致,所有存储在集合中的数据都是BSON格式,
4.3.1、插入文档
使用db.yourCollectionName.insertOne()方法插入单个文档:
test>db.newMyCollection.insertOne({name:"Jhone", age:23})
{
acknowledged: true,
insertedId: ObjectId("654eddadfba0f2e5ddf52465")
}
使用db.yourCollectioinName.insertMany()方法插入多个文档:
test>db.newMyCollection.insertMany([{name:"Jhone", age:21},{name:"Peter", age:20}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("654ede7efba0f2e5ddf52467"),
'1': ObjectId("654ede7efba0f2e5ddf52468")
}
}
4.3.2、查询文档
使用db.yourCollectionName.find()方法查询集合中的文档:
test>db.newMyCollection.find()
[
{ _id: ObjectId("654ed4c0fba0f2e5ddf52464"), name: 'Jhone', age: 23 },
{ _id: ObjectId("654eddadfba0f2e5ddf52465"), name: 'Jhone', age: 23 },
{ _id: ObjectId("654ede65fba0f2e5ddf52466"), name: 'Jhone', age: 21 },
{ _id: ObjectId("654ede7efba0f2e5ddf52467"), name: 'Jhone', age: 21 },
{ _id: ObjectId("654ede7efba0f2e5ddf52468"), name: 'Peter', age: 20 }
]
该方法会显示集合中的所有文档。 如果要查询单个文档,可以使用db.yourCollectionName.findOne()方法:
test>db.newMyCollection.findOne()
{ _id: ObjectId("654ed4c0fba0f2e5ddf52464"), name: 'Jhone', age: 23 }
test>
4.3.3、更新文档
使用db.yourCollectionName.updateOne()方法更新单个文档,例如下面的命令将名字为Jhone的文档中的age更新为34: test>db.newMyCollection.updateOne({name:“Jhone”},{$set:{age:34}}) { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 } db.yourCollectionName.updateMany()方法更新符合条件的多个文档:
test>db.newMyCollection.updateMany({name:"Jhone"}, {$set:{age:34}})
{
acknowledged: true,
insertedId: null,
matchedCount: 4,
modifiedCount: 3,
upsertedCount: 0
}
test>db.newMyCollection.find()
[
{ _id: ObjectId("654ed4c0fba0f2e5ddf52464"), name: 'Jhone', age: 34 },
{ _id: ObjectId("654eddadfba0f2e5ddf52465"), name: 'Jhone', age: 34 },
{ _id: ObjectId("654ede65fba0f2e5ddf52466"), name: 'Jhone', age: 34 },
{ _id: ObjectId("654ede7efba0f2e5ddf52467"), name: 'Jhone', age: 34 },
{ _id: ObjectId("654ede7efba0f2e5ddf52468"), name: 'Peter', age: 20 }
]
4.3.4、删除文档
使用 db.yourCollectionName.deleteOne() 方法删除单个文档:
test>db.newMyCollection.deleteOne({name:"Jhone"})
{ acknowledged: true, deletedCount: 1 }
test>
使用使用 db.yourCollectionName.deleteMany() 方法删除符合条件的多个文档:
test>db.newMyCollection.deleteMany({name:"Jhone"})
{ acknowledged: true, deletedCount: 3 }
test>
5、总结
以上就是MongoDB的单机安装与简单使用,高阶内容敬请期待。 我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。 这一篇是在酒店所写,写了几个小时终于写完,好畅快:
精彩内容