在 Debian 上向 MongoDB 导入数据,常见方式取决于你的数据格式。下面按最常用场景给你说明(假设已在 Debian 上安装好 MongoDB)。
mongorestore 导入 BSON 备份(最常用)mongorestore 命令(通常随 mongodb-org-tools 提供)mongorestore --db mydb /path/to/backup/mydb
mongorestore --db mydb --collection users /path/to/users.bson
mongorestore \
--host 127.0.0.1 --port 27017 \
--username admin --password secret \
--authenticationDatabase admin \
--db mydb /path/to/backup
mongoimport 导入 JSON / CSVmongoimport \
--db mydb \
--collection users \
--file users.json \
--jsonArray
--jsonArray用于数组格式 JSON
mongoimport \
--db mydb \
--collection users \
--type csv \
--headerline \
--file users.csv
mongoimport --drop --db mydb --collection users --file users.json
mongo mydb
db.users.insertMany([
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 }
])
which mongoimport
若没有:
sudo apt install mongodb-org-tools
或(新版):
sudo apt install mongodb-database-tools
sudo systemctl start mongod
sudo systemctl enable mongod
| 数据来源 | 命令 |
|---|---|
| BSON 备份 | mongorestore |
| JSON | mongoimport |
| CSV | mongoimport |
| 手动 | mongo shell |
如果你能告诉我:
我可以给你一条可直接复制执行的命令。