Giter VIP home page Giter VIP logo

mongo-util's Introduction

mongo-util

Dependency Status Gem Version License

Copying over complete databases is not that hard with mongodb. Use mongodump followed by mongorestore and there you go. But say you want to copy only certain records from certain collections, things get a little bit hairy.

Why? Can't I just use some javascript and the mongo-shell? No. Some mongo commands just don't have authentication, so I found it was the easiest way to use mongorestore and mongodump and wrap them into a handy library.

Synopsis

require 'mongo/util'

# Create mongo util instance, set database to fetch from and database to copy to
# as well as dump folder
mongo = Mongo::Util.new({ host: 'foo.mongohosting.com', port: 31337, db: 'foo' },
                        { db: 'foo_development' },
                        'mongo_dump')

# Add authentication
mongo.from = { user: 'root', password: 'password' }

# Iterate over collections
mongo.collections.each do |collection|

  # Add some case here
  if collection == 'some.collection'

    # Add queries here as you like
    mongo.remove!(collection, query: { something: false })
    mongo.dump(collection: collection, query: { something: false })

  else

    # Just copy
    mongo.remove!(collection)
    mongo.dump(collection: collection)

  end

  # Restore & clean up dump folder
  mongo.restore!
  mongo.clean!                                                                                                   
end

The snippet above is replacing the contents of all collections from localhost:27017/foo_development with the ones from foo.mongohosting.com:31337/foo (you can also specify an external db to copy to, localhost:27017 is standard), except for some.collection, for which it only replaces the entries where the query { something: false } matches.

Installing

Install as you would install any other gem. bundler is your friend!

When you're using Rails and you want to use this gem for a script to get fresh production data, just use bundle exec yourscript.rb, where yourscript.rb contains something like above.

Contributing

Fork -> Commit -> Pull Request Pull

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.