mongo-util 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +68 -1
- data/lib/mongo/util/version.rb +1 -1
- data/lib/mongo/util.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ce946a3aab8df1e1933ffd88e4991c992e129a9
|
4
|
+
data.tar.gz: a3dfad61ebc465b5f3dad97fbe5b08586e6eba12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6df06ef07f8ac9ddfb902c09321013bdb314adf2e6ec4739b911a585626be8f244ca185aac6608ac6da2b963f1706300a362f6f8774cc1fb25997df04a683689
|
7
|
+
data.tar.gz: 31a9e1d4d32af7fa52be465b1f07825523513bd110771c4a47a77e74d2a8d33a69d9237ce2ab015d4f6b6700b412e5936e8b0d331d5a4c64916a9823df3538db
|
data/README.md
CHANGED
@@ -1,4 +1,71 @@
|
|
1
1
|
mongo-util
|
2
2
|
==========
|
3
3
|
|
4
|
-
|
4
|
+
[](https://gemnasium.com/tonekk/rails-js)
|
5
|
+
[](https://rubygems.org/gems/rails-js)
|
6
|
+
[](http://tonekk.mit-license.org)
|
7
|
+
|
8
|
+
Copying over complete databases is not that hard with **mongodb**.
|
9
|
+
Use `mongodump` followed by `mongorestore` and there you go.
|
10
|
+
But say you want to copy *only certain records from certain collections*, things get a little bit hairy.
|
11
|
+
|
12
|
+
Why? Can't I just use some javascript and the `mongo`-shell?
|
13
|
+
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.
|
14
|
+
|
15
|
+
|
16
|
+
## Synopsis
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
|
20
|
+
require 'mongo/util'
|
21
|
+
|
22
|
+
# Create mongo util instance, set database to fetch from and database to copy to
|
23
|
+
# as well as dump folder
|
24
|
+
mongo = Mongo::Util.new({ host: 'foo.mongohosting.com', port: 31337, db: 'foo' },
|
25
|
+
{ db: 'foo_development' },
|
26
|
+
'mongo_dump')
|
27
|
+
|
28
|
+
# Add authentication
|
29
|
+
mongo.from = { user: 'root', password: 'password' }
|
30
|
+
|
31
|
+
# Iterate over collections
|
32
|
+
mongo.collections.each do |collection|
|
33
|
+
|
34
|
+
# Add some case here
|
35
|
+
if collection == 'some.collection'
|
36
|
+
|
37
|
+
# Add queries here as you like
|
38
|
+
mongo.dump(collection: collection, query: { something: false }) &&
|
39
|
+
mongo.remove(collection, query: { something: false }) &&
|
40
|
+
mongo.restore
|
41
|
+
|
42
|
+
else
|
43
|
+
|
44
|
+
# Just copy
|
45
|
+
mongo.dump(collection: collection) &&
|
46
|
+
mongo.remove(collection) &&
|
47
|
+
mongo.restore
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
# Clean up dump folder
|
52
|
+
mongo.clean
|
53
|
+
end
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
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.
|
58
|
+
|
59
|
+
|
60
|
+
## Installing
|
61
|
+
|
62
|
+
Install as you would install any other gem.
|
63
|
+
[bundler](http://bundler.io/) is your friend!
|
64
|
+
|
65
|
+
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.
|
66
|
+
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
[Fork](https://github.com/tonekk/mongo-util/fork) -> Commit -> Pull Request
|
71
|
+
Pull
|
data/lib/mongo/util/version.rb
CHANGED
data/lib/mongo/util.rb
CHANGED
@@ -74,7 +74,11 @@ module Mongo
|
|
74
74
|
raise 'Cannot fetch collections: needs @to[:host], @to[:port], @to[:db]'
|
75
75
|
end
|
76
76
|
|
77
|
-
|
77
|
+
cmd = "mongo #{@to[:db]} --host #{@to[:host]} --port #{@to[:port]} --quiet --eval 'db.getCollectionNames()'"
|
78
|
+
# Append auth, if neccessary
|
79
|
+
cmd += Mongo::Util.authentication(@to)
|
80
|
+
|
81
|
+
`#{cmd}`.rstrip.split(',')
|
78
82
|
end
|
79
83
|
|
80
84
|
# Deletes @dump_dir
|