MongoDB--导出数据
参考:https://www.mongodb.com/docs/database-tools/mongodump/
mongodump --archive=test.20150715.archive --db=test
客户端:
参考: https://stackoverflow.com/questions/25947929/how-to-list-all-databases-in-the-mongo-shell
mongo即可
show dbs
use <db_name>
show collections (它跟 show tables 返回的是一样的)
导入
mongoimport --db <db-name> --collection <collection-name> --jsonArray <your-path.json>
node driver
声明document:
use lueluelue ( 这个就是create database )
查询
db.my_table.find() # 查询所有 , my_table就是表名, 记得这里在代码中需要 .toArray()
db.my_table.findOne() 只查询一个。
插入
> db.domains.insert({"address": "168EsqUaRF6teT9enPx9X6dbHR7JbWN5hDeNAKtHGUPh4RCy", "name": "goodgoodstudy013"})
更新
611 let all_domains = await domains.find().toArray()
612
613 all_domains.forEach( async function(e: Domain) {
614
615 let updateDoc = {
616 $set: {
617 batch: (random(2) + 1),
618 chain: (random(2) == 0 ? "litentry" : "coinversation")
619 }
620 }
621 await domains.updateOne({_id: e._id}, updateDoc)
622 })
查询
await redeem_codes.findOne({ shortcode: shortcode })
首先看一下27017是否正常运行,因为MongoDB是在27017进行运行的,telnet 127.0.0.1 27017
ubuntu@VM-0-13-ubuntu:/opt/app$ telnet 127.0.0.1 27017
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]
telnet> ^CConnection closed.
然后进行查看,27017都运行什么进程,显示是有三个进程
ubuntu@VM-0-13-ubuntu:/opt/app/lueluelue-backend-fastify/current$ lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 7034 ubuntu 18u IPv4 67490 0t0 TCP localhost:33604->localhost:27017 (ESTABLISHED)
node 7034 ubuntu 19u IPv4 68353 0t0 TCP localhost:33608->localhost:27017 (ESTABLISHED)
node 7034 ubuntu 20u IPv4 68553 0t0 TCP localhost:33674->localhost:27017 (ESTABLISHED)
ruby 425436 ubuntu 22u IPv4 3185129 0t0 TCP localhost:39706->localhost:27017 (ESTABLISHED)
ruby 425436 ubuntu 23u IPv4 3185137 0t0 TCP localhost:39710->localhost:27017 (ESTABLISHED)
ruby 425436 ubuntu 24u IPv4 3184026 0t0 TCP localhost:39714->localhost:27017 (ESTABLISHED)
ruby 425436 ubuntu 27u IPv4 3190168 0t0 TCP localhost:40040->localhost:27017 (ESTABLISHED)
node 712724 ubuntu 18u IPv4 36153083 0t0 TCP localhost:58296->localhost:27017 (ESTABLISHED)
node 712724 ubuntu 19u IPv4 36153193 0t0 TCP localhost:58316->localhost:27017 (ESTABLISHED)
node 712724 ubuntu 20u IPv4 36150642 0t0 TCP localhost:58302->localhost:27017 (ESTABLISHED)
ps -ef | grep mongo
systemd+ 5647 5627 0 Jun06 ? 03:07:02 mongod --bind_ip_all
root 213980 213955 0 Jun24 ? 00:00:43 mongo
root 463340 462704 0 Jun08 pts/1 00:01:11 mongo
ubuntu 1700084 1699862 0 10:01 pts/1 00:00:00 grep --color=auto mongo
cd /proc/7034
ubuntu@VM-0-13-ubuntu:/proc/7034$ ll
total 0
dr-xr-xr-x 9 ubuntu ubuntu 0 Jul 18 06:06 ./
dr-xr-xr-x 199 root root 0 Jun 6 16:12 ../
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 10:01 arch_status
dr-xr-xr-x 2 ubuntu ubuntu 0 Jul 18 10:01 attr/
-rw-r--r-- 1 ubuntu ubuntu 0 Jul 18 10:01 autogroup
-r-------- 1 ubuntu ubuntu 0 Jul 18 10:01 auxv
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 10:01 cgroup
--w------- 1 ubuntu ubuntu 0 Jul 18 10:01 clear_refs
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 06:06 cmdline
-rw-r--r-- 1 ubuntu ubuntu 0 Jul 18 10:01 comm
-rw-r--r-- 1 ubuntu ubuntu 0 Jul 18 10:01 coredump_filter
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 10:01 cpuset
lrwxrwxrwx 1 ubuntu ubuntu 0 Jul 18 07:40 cwd -> /opt/app/lueluelue-backend-fastify/new_claim_20220501/
-r-------- 1 ubuntu ubuntu 0 Jul 18 10:01 environ
cd /proc/712724
ubuntu@VM-0-13-ubuntu:/proc/712724$ ll
total 0
dr-xr-xr-x 9 ubuntu ubuntu 0 Jul 18 06:06 ./
dr-xr-xr-x 203 root root 0 Jun 6 16:12 ../
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 10:02 arch_status
dr-xr-xr-x 2 ubuntu ubuntu 0 Jul 18 10:02 attr/
-rw-r--r-- 1 ubuntu ubuntu 0 Jul 18 10:02 autogroup
-r-------- 1 ubuntu ubuntu 0 Jul 18 10:02 auxv
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 10:02 cgroup
--w------- 1 ubuntu ubuntu 0 Jul 18 10:02 clear_refs
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 06:06 cmdline
-rw-r--r-- 1 ubuntu ubuntu 0 Jul 18 10:02 comm
-rw-r--r-- 1 ubuntu ubuntu 0 Jul 18 10:02 coredump_filter
-r--r--r-- 1 ubuntu ubuntu 0 Jul 18 10:02 cpuset
lrwxrwxrwx 1 ubuntu ubuntu 0 Jul 18 07:40 cwd -> /opt/app/lueluelue-backend-fastify/releases/20220401074311/
-r-------- 1 ubuntu ubuntu 0 Jul 18 10:02 environ
udo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0404f4be99a2 postgres:14.2 "docker-entrypoint.s…" 5 weeks ago Up 5 weeks 127.0.0.1:5432->5432/tcp keen_kilby
3f1d593f54d2 postgres:14.2 "docker-entrypoint.s…" 5 weeks ago Exited (1) 5 weeks ago objective_bhaskara
29b06cdf3b26 mongo "docker-entrypoint.s…" 5 weeks ago Up 5 weeks 127.0.0.1:27017->27017/tcp dreamy_darwin
c8eb7b3234d6 postgres:14.2 "docker-entrypoint.s…" 5 weeks ago Exited (0) 5 weeks ago vigorous_curie
a1d96b0835f4 postgres:14.2 "docker-entrypoint.s…" 2 months ago Exited (0) 5 weeks ago wizardly_joliot
75204b0a6b92 mongo "docker-entrypoint.s…" 3 months ago Exited (0) 5 weeks ago sharp_bardeen
cd /proc/5647
ubuntu@VM-0-13-ubuntu:/proc/5647$ ll
ls: cannot read symbolic link 'cwd': Permission denied
ls: cannot read symbolic link 'root': Permission denied
ls: cannot read symbolic link 'exe': Permission denied
total 0
dr-xr-xr-x 9 systemd-coredump systemd-coredump 0 Jul 18 06:06 ./
dr-xr-xr-x 199 root root 0 Jun 6 16:12 ../
-r--r--r-- 1 systemd-coredump systemd-coredump 0 Jul 18 10:03 arch_status
dr-xr-xr-x 2 systemd-coredump systemd-coredump 0 Jul 18 10:03 attr/
-rw-r--r-- 1 systemd-coredump systemd-coredump 0 Jul 18 10:03 autogroup
-r-------- 1 systemd-coredump systemd-coredump 0 Jul 18 10:03 auxv
-r--r--r-- 1 systemd-coredump systemd-coredump 0 Jul 18 07:40 cgroup
/proc/5647$ sudo docker exec -it 29b0 bash
root@29b06cdf3b26:/# mongo
MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d7ae6cd4-58ab-4973-b55b-0f52f2006aeb") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
2022-06-06T08:25:36.871+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-06-06T08:25:37.279+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
lueluelue 0.003GB
test 0.000GB
> use lueluelue
switched to db lueluelue
> show tables
claims
domain_registers
domains
invalid_domains
redeem_codes
simple_redeem_codes
> db.claims.findAll()
uncaught exception: TypeError: db.claims.findAll is not a function :
@(shell):1:1
> db.simple_redeem_codes.find()
{ "_id" : ObjectId("626736e9c51cf50d3d7a3ffd"), "code" : "111222333444", "is_used" : false, "comment" : "给精神领袖一号用", "created_at" : ISODate("2022-04-26T00:03:53.221Z"), "updated_at" : ISODate("2022-04-26T00:03:53.221Z") }
Type "it" for more
> it
> it
no cursor
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
lueluelue 0.003GB
test 0.000GB
> use lueluelue
switched to db lueluelue
> mongo
Mongo( MongoBridge( MongoRunner(
> mongo^C
bye
root@29b06cdf3b26:/# mongo
mongo mongod mongodump mongoexport mongofiles mongoimport mongorestore mongos mongosh mongostat mongotop
root@29b06cdf3b26:/# mongo^C
root@29b06cdf3b26:/# mongodump --archive=20220718.archive --db=lueluelue
2022-07-18T02:10:07.720+0000 writing lueluelue.simple_redeem_codes to archive 'lueluelue-mongo.20220718.archive'
2022-07-18T02:10:07.721+0000 writing lueluelue.claims to archive 'lueluelue-mongo.20220718.archive'
2022-07-18T02:10:07.722+0000 writing lueluelue.domains to archive 'lueluelue-mongo.20220718.archive'
2022-07-18T02:10:07.726+0000 writing lueluelue.domain_registers to archive 'lueluelue-mongo.20220718.archive'
2022-07-18T02:10:07.743+0000 done dumping lueluelue.simple_redeem_codes (470 documents)
2022-07-18T02:10:07.750+0000 writing lueluelue.redeem_codes to archive 'lueluelue-mongo.20220718.archive'
2022-07-18T02:10:07.754+0000 done dumping lueluelue.domains (3702 documents)
2022-07-18T02:10:07.755+0000 done dumping lueluelue.claims (1136 documents)
2022-07-18T02:10:07.755+0000 writing lueluelue.invalid_domains to archive 'lueluelue-mongo.20220718.archive'
2022-07-18T02:10:07.767+0000 done dumping lueluelue.domain_registers (5200 documents)
2022-07-18T02:10:07.775+0000 done dumping lueluelue.invalid_domains (59 documents)
2022-07-18T02:10:07.803+0000 done dumping lueluelue.redeem_codes (300 documents)
root@29b06cdf3b26:/# ls
bin data docker-entrypoint-initdb.d home lib lib64 media opt lueluelue0716.archive root sbin sys usr
boot dev etc js-yaml.js lib32 libx32 mnt 20220718.archive proc run srv tmp var
sudo docker cp 29b0:/20220718.archive .
ll -altrh
drwxrwxr-x 2 ubuntu ubuntu 4.0K Apr 26 15:25 json0426
-rw-r--r-- 1 root root 935K Apr 27 15:05 20220427.archive
scp ubuntu@api-rpc.lueluelue.link:20220718.archive /workspace/20220718.archive
20220718.archive 100% 2173KB 51.6KB/s 00:42