【MongoDB】json文件导出和导入

1.导出特定集合 到json文件 

mongoexport -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin --db  test --collection users   --type=json --out /mongodb/backup/mongooutput.json

--使用管理员用户导致指定数据库的指定集合的数据到json中。
[mongod@mysql57 backup]$ mongoexport -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin --db  test --collection users   --type=json --out /mongodb/backup/mongooutput.json
2024-06-14T11:12:53.839+0800	connected to: 192.168.75.56:27017
2024-06-14T11:12:53.841+0800	exported 21 records

--导出数据查看。
[mongod@mysql57 backup]$ cat mongooutput.json 
{"_id":{"$oid":"666a6175a0184f8213fade86"},"FName":"Test User","Age":45.0,"Gender":"F","Country":"US"}
{"_id":{"$oid":"666a617da0184f8213fade87"},"Name":"Test User1","Age":11.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade88"},"Name":"Test User2","Age":12.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade89"},"Name":"Test User3","Age":13.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade8a"},"Name":"Test User4","Age":14.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade8b"},"Name":"Test User5","Age":15.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade8c"},"Name":"Test User6","Age":16.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade8d"},"Name":"Test User7","Age":17.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade8e"},"Name":"Test User8","Age":18.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade8f"},"Name":"Test User9","Age":19.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade90"},"Name":"Test User10","Age":20.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade91"},"Name":"Test User11","Age":21.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade92"},"Name":"Test User12","Age":22.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade93"},"Name":"Test User13","Age":23.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade94"},"Name":"Test User14","Age":24.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade95"},"Name":"Test User15","Age":25.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade96"},"Name":"Test User16","Age":26.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade97"},"Name":"Test User17","Age":27.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade98"},"Name":"Test User18","Age":28.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade99"},"Name":"Test User19","Age":29.0,"Gender":"F","Country":"India"}
{"_id":{"$oid":"666a617da0184f8213fade9a"},"Name":"Test User20","Age":30.0,"Gender":"F","Country":"India"}

2.json文件导入到指定的集合中 

mongoimport -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin --db  test --collection users   --type=json --file /mongodb/backup/mongooutput.json

[mongod@mysql57 backup]$ mongoimport -u admin -p admin --host 192.168.75.56 --port 27017 --authenticationDatabase admin --db  test --collection users   --type=json --file /mongodb/backup/mongooutput.json
2024-06-14T11:20:26.183+0800	connected to: 192.168.75.56:27017
2024-06-14T11:20:26.239+0800	num failures: 21
2024-06-14T11:20:26.239+0800	error inserting documents: multiple errors in bulk operation:
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade87') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade88') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade89') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8a') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8b') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8c') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8d') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8e') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade8f') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade90') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade91') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a6175a0184f8213fade86') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade92') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade94') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade95') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade96') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade97') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade93') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade99') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade9a') }
  - E11000 duplicate key error collection: test.users index: _id_ dup key: { : ObjectId('666a617da0184f8213fade98') }

2024-06-14T11:20:26.239+0800	imported 0 documents
--由此可见,默认导入并不会覆盖。需要加入drop 选项。

--先删除后插入。
mongoimport -u admin -p admin --host 192.168.75.56 --port 27017 --drop --authenticationDatabase admin --db  test --collection users   --type=json --file /mongodb/backup/mongooutput.json
[mongod@mysql57 backup]$ mongoimport -u admin -p admin --host 192.168.75.56 --port 27017 --drop --authenticationDatabase admin --db  test --collection users   --type=json --file /mongodb/backup/mongooutput.json
2024-06-14T11:21:32.900+0800	connected to: 192.168.75.56:27017
2024-06-14T11:21:32.930+0800	dropping: test.users
2024-06-14T11:21:32.986+0800	imported 21 documents

由此可见,有集合冲突时,可以使用--drop 选项先删除之前的集合,然后创建集合插入新的数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值