MongoDB synchronize Development and Production databases

余生长醉 提交于 2019-12-05 08:58:11

You can use the mongoexport tool to export the single collection from your development database. Use it in conjunction with a --query option where you can express a predicate. For example something such as ${ts : {$gt : previous clone time}}.

Then, use mongoimport to import your delta file into production database. Use --upsert and --upsertFields if you have two different logical documents with different _id values, but express the same document

@Orid, thanks for your answer, and sorry for the late reply.

After a lot of research and some trial-error, I decided to use the solution stated in the question (Copy test DB to machine, and then copy the collections one by one). This is because the data I'm using here is static data, that doesn't have a real reason to have a timestamp. Also, I've decided to waive the requirement for "only updating", so for now I'm using mongorestore with --drop

I do all this using this script:

  1. The shell script file:

rm -rf dump/;

mongo copyTestDb.js;

for COLLECTION in <Collections>
do
mongodump -d nutrino_copy -c $COLLECTION -o dump
mongorestore -d nutrino -c ${COLLECTION} --drop dump/nutrino_copy/${COLLECTION}.bson
done
  1. The js script file:

    db.copyDatabase("<dbName>","<dbName_Copy>","<testMachineUrl>")

Do you think I should use MongoImport instead of MongoRestore?

Check out mongo-sync


It's a script I wrote for my self when I had to constantly copy my Local MongoDB database to and from my Production DB for a Project (I know it's stupid).

Once you put your DB details in config.yml, you can start syncing using two simple commands:

./mongo-sync push       # Push DB to Remote
./mongo-sync pull       # Pull DB to Local

If you use it inside some project, it's a good idea to add config.yml to .gitignore


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!