Mongodb: db.collection.copyTo() and eval() have been deprecated. What's the alternatives? - mongodb

I find that db.collection.copyTo() and eval() have been deprecated since 3.0. But I do not find what can be instead.
What's the alternatives?

For a server-side solution you can use aggregation...
db.getCollection('source')
.aggregate([
{ $out: 'destination' }
]);

Per this discussion on the MongoDB Group.
The alternative is to implement the equivalent queries/operations using the normal MongoDB query language and client driver API
So that would mean writing your queries in a client environment (e.g. Node.js) and execute them that way. If run on the server connecting to localhost, they should pretty quick, although probably not as quick as using eval.
The rationale for the being deprecated is outlined in this ticket.
https://jira.mongodb.org/browse/SERVER-17453

Create a mongodump of that collection, do mongorestore to a separate or new collection.
This wont stop read/write or wont lock the collection.
mongodump --db db-name --collection collection-name --archive=collection-name.archive
or save as json both works
If saved as archive, to restore
mongorestore --db db-name --collection collection-name --archive=collection-name.archive

Related

Which one is the preferred choice Mongodump VS Mongoexport for upgrading mongoDB database?

My customer uses mongoDB 2.4 and as there are some limitations with this version, we have give them the option to upgrade to Latest stable mongoDB 3.4.5.
Initial testing of using
mongodump in MongoDB 2.4
and mongorestore in Mongodb 3.4.5 worked fine as I can see all the collections imported.
From the documentation mongorestore it was not mentioned anywhere that it can restore the dumps from older versions of mongoDB.
As we cannot use the mongorestore , Can I use "mongoexport" to export the data in csv/json format of older mongoDB 2.4 , and import into newer version of mongoDB 3.4 ?
What are the possible problems of using "mongoexport/mongoimport" instead of "mongodump" to upgrade to newer version of mongoDB 3.4 ?
NOTE: I will remove the older version of mongoDB completely and will install the newer version of mongoDB
Mongodump and Mongorestore are better because:
They run faster
They preserve some data formats better than mongoexport and mongoimport, because the data is not translated from BSON into JSON and back.
As described in the MongoDB Docs on MongoImport:
WARNING
Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.
In addition, be very careful about the upgrade using mongorestore; just because the data is restored as it was previously, this does not mean that the new version of MongoDB can work with it. For example there were a sequence of changes to the authorisation model after v2.4 which means that you must first upgrade to v2.6, and only then to v3.0. There are similar structural changes at each major version, so it is recommended that you upgrade stepwise, one major version at a time i.e.
v2.4 -> v2.6
v2.6 -> v3.0
v3.0 -> v3.2
v3.2 -> v3.4
From http://www.dba86.com/docs/mongo/2.4/core/import-export.html, mongoexport is supported from 2.4 version. Hence it should be the right tool for that. But still the document has warning message as well.
Warning:
Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data
types, because JSON can only represent a subset of the types supported
by BSON. Use mongodump and mongorestore as described in MongoDB Backup
Methods for this kind of functionality.
Hope that helps!!!!
Both tools (by default) will just walk the _id index to fetch the data and then write it out to disk. So, yes, both tools will similarly impact your working set which is why I would generally recommend running them against a secondary (preferably a hidden secondary if possible).
I assume you are looking for a mongodump equivalent of the --fields option from mongoexport to only dump out specific fields. The query option can be used to filter results, but it cannot be used with a projection (to select the fields returned) - this is a feature request that is being tracked in TOOLS-28 but is not yet scheduled.

MongoDB migration

Hello I have an ubuntu 14.04 server that is running mongodb 2.4.14. I need to move the mongo instance to a new server. I have installed mongo 3.4.2 on the new server and need to move the databases over. I am pretty new with mongo. I have 2 databases that are pretty big but when I do a mongo dump the file is nowhere near the site of the databases that mongo is showing.I cannot figure out how to get mongoexport to work. What would be the best way to move those databases? If possible can we just export the data from mongo and then import it?
You'll need to give more information on your issue with mongodump and what mongodump parameters you were using.
Since you are doing a migration, you'll want to use mongodump and not mongoexport. mongoexport only outputs a JSON/CSV format of a collection. Because of this, mongoexport cannot retain certain datatypes that exist in BSON and thus MongoDB does not suggest that anyone uses mongoexport for full backups; this consideration is listed on mongo's site.
mongodump will be able to accurately create a backup of your database/collection that mongorestore will be able to restore that dump to your new server.
If you haven't already, check out Back Up and Restore with MongoDB Tools

Export number of Documents from mongodb

I use mongochef as a UI client for my mongo database. Now I have collection which consists of 12,000 records. I want to export them using the mongochef.
I have tried with the export option(available) which is working fine up to 3000 documents. But if the number of records gets increasing the system is getting hung up.
Can you please let me know the best way to export all the documents in a nice way using mongochef.
Thanks.
Finally I came to conclusion to use the mongo using terminal which the best way to use(efficient).
read about the primary and secondary databases and executed the following query:
mongoexport --username user --password pass --host host --db database --collection coll --out file_name.json

Migrate mongodb collections and indexes excluding data

I have two environments DEV and STAGE and I need to take all of my collections from a DEV database to a database in STAGE.
Currently using mongodump to get all of my collections and indexes, which appears to work very well.
>mongodump --host 192.168.#.# --port 27019 --db MyDB
Then I am using mongorestore to populate STAGE with the appropriate collections and indexes.
>mongorestore --host 192.168.#.# --port 27019 --db test "C:\Program Files\MongoDB 2.6 Standard\bin\dump\MyDB"
My collections and indexes come across perfectly, however, my collection content comes as well. I have not found a way to exclude my actual data inside my collections...is this doable? Can I remove files from the output of mongodump to only have my collections and their 'indexes'.
MongoDB is schemaless so basically there is no point of restoring empty collections. Collections are created on the fly when you do a write.
I understand you just want to restore collection's metadata as indexes etc., I don't know what driver are you using but I would suggest you deal with this problem on the application level by writing a routine that creates the indexes etc.
Also removing the bson files and keeping only the metadata files from mongodump as you suggest will work (or at least it works in my case with mongo V 3.0.5 with wiredtiger engine) but is not documented as far as I know.
An other alternative could be to use -query option on mongodump to specify documents to include i.e: {_id:a_non_existing_id} but this option is applicable only to collection level dumps.

Creating a subset of an existing mongo db database

This query is related to building a small MongoDB test database from a large existing database.
My plan to execute this is as follows:
a) Use mongodump with an aggregate query which specifies my conditions for the records to be copied over to the test database.
Will this idea work? From what I have read on forums, using a MongoDB query as is in a mongodump command will not work.
Any guidance on this is most appreciated.
You can use the following command to get the subset of the DB.
mongodump --query "your query here"
For more information read the mongodump documentation here.