Skip to content

Instantly share code, notes, and snippets.

@cybertoast
Created May 13, 2013 20:35
Show Gist options
  • Save cybertoast/5571293 to your computer and use it in GitHub Desktop.
Save cybertoast/5571293 to your computer and use it in GitHub Desktop.
MongoDB: export and import a filtered set of documents
#!/bin/bash
set -ex
now=$(date -u +%Y%m%d%H%M%S)
dest_dir="/tmp/"
dest_file="query_$now.json"
# Export your documents with a query
mongoexport --db YOURDB --username USERNAME --password PASSWORD --collection media \
--query '{"media_for._ref.$id": { $in: [ ObjectId("4e9f46c96e48caa47f0006c7"), ... ] }}' \
--jsonArray --out $dest_dir$dest_file
# If you want to just see the content in a nice format:
# cat $dest_dir$dest_file | python -mjson.tool
# DO SOME PROCESSING STUFF
magic_stuff_goes_here
# Import the previously exported documents
mongoexport --db YOURDB --username USERNAME --password PASSWORD --collection media \
--jsonArray --file $dest_dir$dest_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment