SQL 术语 / 概念 | MongoDB 术语 / 概念 | 解释 / 说明 |
---|---|---|
database | database | 数据库 |
table | collection | 数据库表 / 集合 |
row | document | 数据记录行 / 文档 |
column | field | 数据字段 / 域 |
index | index | 索引 |
table joins | 表连接,MongoDB 不支持 | |
primary key | primary key | 主键,MongoDB 自动将_id 字段设置为主键 |
dnf -y install openssl libcurl
tar zxvf mongodb-linux-x86_64-rhel8-8.0.8.tgz
mv mongodb-linux-x86_64-rhel8-8.0.8.tgz /usr/local/mongodb
cd /usr/local/mongodb
cd bin/
vim /etc/profile
在末尾添加一行 : export /usr/local/mongodb/bin:$PATH
source /etc/profile
[root@localhost ~]#mkdir -p /var/lib/mongo
[root@localhost ~]#mkdir -p /var/log/mongodb
[root@localhost ~]# dnf install -y gcc make perl
[root@localhost ~]# tar xzf openssl-1.1.1w.tar.gz
[root@localhost ~]# cd openssl-1.1.1w
[root@sentinel01 openssl-1.1.1w]#./config --prefix=/opt/openssl11 --openssldir=/opt/openssl11/ssl
[root@sentinel01 openssl-1.1.1w]#make -j2
[root@sentinel01 openssl-1.1.1w]#make install
[root@localhost ~]# echo 'export LD_LIBRARY_PATH=/opt/openssl11/lib:$LD_LIBRARY_PATH' | sudo tee /etc/profile.d/openssl11.sh
[root@localhost ~]# source /etc/profile.d/openssl11.sh
[root@localhost ~]# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
[root@localhost ~]# mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 12102
child process started successfully, parent exiting
[root@localhost ~]# tar xzf mongosh-2.5.0-linux-x64-openssl3.tgz
[root@localhost ~]# cd mongosh-2.5.0-linux-x64-openssl3
[root@localhost mongosh-2.5.0-linux-x64-openssl3]# cd bin/
[root@localhost bin]# cp mongosh /usr/local/bin/
[root@localhost bin]# cp mongosh_crypt_v1.so /usr/local/lib/
[root@localhsot bin]# mongosh
Current Mongosh Log ID: 680a15327aa1c07b1b26ff
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.0
Using MongoDB: 8.0.8
Using Mongosh: 2.5.0For mongosh info see:
The server generated these startup warnings when booting
2025-04-24T18:22:54.368+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See Production Notes for Self-Managed Deployments - Database Manual - MongoDB Docs
2025-04-24T18:22:55.892+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2025-04-24T18:22:55.892+08:00: You are running this process as the root user, which is not recommended2025-04-24T18:22:55.892+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip 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
2025-04-24T18:22:55.892+08:00: Soft rlimits for open file descriptors too low
2025-04-24T18:22:55.892+08:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
2025-04-24T18:22:55.892+08:00: We suggest setting the contents of sysfsFile to 0.
2025-04-24T18:22:55.892+08:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.test>
>show dbs
>db
test> use runoob
switched to db runoob
runoob> db
runoob
runoob>
runoob> show dbs
admin 40.00 KiB
config 60.00 KiB
local 40.00 KiB
runoob>
runoob> db.runoob.insertOne({"name":"zhangsan"})
{
acknowledged: true,
insertedId: ObjectId('680a18f4277aa1c07b1b2700')
}
runoob> show dbs
admin 40.00 KiB
config 60.00 KiB
local 40.00 KiB
runoob 40.00 KiB
runoob>
runoob>use test
test>db.dropDatabase()
>show collections
db.createCollection("myComplexCollection", { capped: true, size: 10485760, max: 5000, validator: { $jsonSchema: { bsonType: "object", required: ["name", "email"], properties: { name: { bsonType: "string", description: "必须为字符串且为必填项" }, email: { bsonType: "string", pattern: ".*@.*$", description: "必须为有效的电子邮件地址" } } }}, validationLevel: "strict", validationAction: "error", storageEngine: { wiredTiger: { configString: "block_compressor=zstd" } }, collation: { locale: "en", strength: 2 } });
> use test switched to db test > db.createCollection("runoob") { "ok" : 1 }
db.adminCommand({ renameCollection: "test.oldCollection", to: "test.newCollection" });
db.adminCommand({ renameCollection: "test.oldCollection", to: "production.newCollection" });
>db.kgc.drop()
db.myCollection.insertOne({ name: "Alice", age: 25, city: "New York" }); { "acknowledged": true, "insertedId": ObjectId("60c72b2f9b1d8b5a5f8e2b2d") }
db.myCollection.insertMany([ { name: "Bob", age: 30, city: "Los Angeles" }, { name: "Charlie", age: 35, city: "Chicago" }]); 返回结果: { "acknowledged": true, "insertedIds": [ ObjectId("60c72b2f9b1d8b5a5f8e2b2e"), ObjectId("60c72b2f9b1d8b5a5f8e2b2f") ] }
>db.myCollection.find();
>db.myCollection.findOne({name:"bob"});
>db.myCollection.deleteOne({name:"bob"});
> db.myCollection.deleteMany({name:"Alice"})
>db.myCollection.insertMany([{ name: "Alice",age: 25,city: "Los Angeles",statu s: "inactive" },{ name: "Bob",age: 30,city: "Los Angeles",status: "active" }, { name: "Charlie",age: 35,city: "Chicago",status: "active"}]);
>db.myCollection.updateOne( { name: "Alice" }, // 过滤条件 { $set: { age: 26 } }, // 更新操作 { upsert: false } // 可选参数 );
db.myCollection.updateMany( { age: { $lt: 30 } }, // 过滤条件 { $set: { status: "active" } }, // 更新操作 { upsert: false } // 可选参数 );
[root@localhost ~]#rpm -ivh mongodb-database-tools-rhel70-x86_64-100.12.0.rpm
[root@localhost ~]# mongodump 2025-04-25T22:44:21.307+0800 writing admin.system.version to dump/admin/system.version.bson 2025-04-25T22:44:21.307+0800 done dumping admin.system.version (1 document) 2025-04-25T22:44:21.308+0800 writing mydb.myCollection to dump/mydb/myCollection.bson 2025-04-25T22:44:21.308+0800 writing test.myComplexCollection to dump/test/myComplexCollection.bson 2025-04-25T22:44:21.310+0800 writing test.myCollection to dump/test/myCollection.bson 2025-04-25T22:44:21.313+0800 done dumping test.myCollection (3 documents) 2025-04-25T22:44:21.313+0800 done dumping test.myComplexCollection (0 documents) 2025-04-25T22:44:21.334+0800 done dumping mydb.myCollection (3 documents) [root@localhost ~]# ls dump
[root@localhost ~]# mongorestore 2025-04-25T22:50:28.111+0800 using default 'dump' directory 2025-04-25T22:50:28.111+0800 preparing collections to restore from 2025-04-25T22:50:28.111+0800 don't know what to do with file "dump/prelude.json", skipping... 2025-04-25T22:50:28.111+0800 reading metadata for mydb.myCollection from dump/mydb/myCollection.metadata.json 2025-04-25T22:50:28.111+0800 reading metadata for test.myCollection from dump/test/myCollection.metadata.json 2025-04-25T22:50:28.111+0800 reading metadata for test.myComplexCollection from dump/test/myComplexCollection.metadata.json 2025-04-25T22:50:28.125+0800 restoring test.myCollection from dump/test/myCollection.bson 2025-04-25T22:50:28.134+0800 restoring mydb.myCollection from dump/mydb/myCollection.bson 2025-04-25T22:50:28.136+0800 finished restoring test.myCollection (3 documents, 0 failures) 2025-04-25T22:50:28.140+0800 restoring test.myComplexCollection from dump/test/myComplexCollection.bson 2025-04-25T22:50:28.144+0800 finished restoring mydb.myCollection (3 documents, 0 failures) 2025-04-25T22:50:28.151+0800 finished restoring test.myComplexCollection (0 documents, 0 failures) 2025-04-25T22:50:28.151+0800 no indexes to restore for collection mydb.myCollection 2025-04-25T22:50:28.151+0800 no indexes to restore for collection test.myCollection 2025-04-25T22:50:28.151+0800 no indexes to restore for collection test.myComplexCollection 2025-04-25T22:50:28.151+0800 6 document(s) restored successfully. 0 document(s) failed to restore.