Skip to content

Instantly share code, notes, and snippets.

@afonsoaugusto
Last active August 26, 2020 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afonsoaugusto/0e25f9e29ae99dcf4110a27871b7fb1f to your computer and use it in GitHub Desktop.
Save afonsoaugusto/0e25f9e29ae99dcf4110a27871b7fb1f to your computer and use it in GitHub Desktop.
Up mongodb with docker and files from backup.
tar -xvf archive.tar.gz
#Subir o banco no docker com bacup do Atlas:
docker run --rm -it -v $PWD:/data/db -p 27017:27017 mongo:3.6
docker run --rm -it -v $PWD:/data/db --memory="5g" --memory-swap="2g" --cpus="2.0" -p 27017:27017 mongo:3.6
function objectIdWithTimestamp(timestamp) {
/* Convert string date to Date object (otherwise assume timestamp is a date) */
if (typeof(timestamp) == 'string') {
timestamp = new Date(timestamp);
}
/* Convert date object to hex seconds since Unix epoch */
var hexSeconds = Math.floor(timestamp/1000).toString(16);
/* Create an ObjectId with that hex timestamp */
var constructedObjectId = ObjectId(hexSeconds + "0000000000000000");
return constructedObjectId
}
db.collection.aggregate([
{$match: { _id: { $gt: objectIdWithTimestamp('2020/03/01') } }},
{$out: 'collection1'}
]);
db.collection.find({ _id: { $gt: objectIdWithTimestamp('2020/08/21') } }).count();

3 - export local

mongoexport \
    --uri mongodb://localhost:27017/db \
    --collection collection3 \
    --type json \
    --out collection.json

4 - import no banco de destino

4-1 - remover collection antiga

db.getSiblingDB("db")
  .getCollection("collection").drop()

4-2 - import da collection nova

mongoimport \
    --uri mongodb+srv://user:pwd@mongo.mongodb.net/db \
    --collection collection \
    --type json \
    --file collection.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment