Shatya Kesarwani
Shatya Kesarwani

Reputation: 73

how to convert mongoDB Oplog file into actual query

I want to convert the MongoDB local Oplog file into an actual real query so I can execute that query and get the exact copy database. Is there any package, file, build-in tools, or script for it?

Upvotes: 2

Views: 543

Answers (3)

Forkmohit
Forkmohit

Reputation: 753

You can get the query from the Oplogs. Oplog defines multiple op types, for instance op: "i","u", "d" etc, are for insert, update, delete. For these types, check the "o"/"o2" fields which have corresponding data and filters. Now based on the op types call the corresponding driver APIs db.collection.insert()/update()/delete().

Upvotes: 0

Kislay Kumar
Kislay Kumar

Reputation: 56

It's not possible to get the exact query from the oplog entry because MongoDB doesn't save the query.

The oplog has an entry for each atomic modification performed. Multi-inserts/updates/deletes performed on the mongo instance using a single query are converted to multiple entries and written to the oplog collection. For example, if we insert 10,000 documents using Bulk.insert(), 10,000 new entries will be created in the oplog collection. Now the same can also be done by firing 10,000 Collection.insertOne() queries. The oplog entries would look identical! There is no way to tell which one actually happened.

Upvotes: 3

JJussi
JJussi

Reputation: 1580

Sorry, but that is impossible.

The reason is that, that opLog doesn't have queries. OpLog includes only changes (add, update, delete) to data, and it's there for replication and redo.

To get an exact copy of DB, it's called "replication", and that is of course supported by the system.

To "replicate" changes to f.ex. one DB or collection, you can use https://www.mongodb.com/docs/manual/changeStreams/.

Upvotes: 0

Related Questions