0

I am new to MongoDB and trying to migrate data between two. So I can connect to DB A and query - but my question is how can I connect to B and copy content from A -> B ? I found this

mongoengine connection and multiple databases

but it states that models are specific to each database to use the meta tag.

Setting up two connections and copying the same model rather than dumping A to disk and reading into B. I have no CLI access to both, so I can not pipe the file in (if that's an option at all like with SQL), hence thinking of a trigger in my FastAPI on B to pull from A.

El Dude
  • 5,328
  • 11
  • 54
  • 101

1 Answers1

2

I think it's probably easier to use pymongo directly for this operation as is way easier and you don't need to parse the doc in a mongoengine model.

c1 = MongoClient('mongodb://host1:27017/')
c2 = MongoClient('mongodb://host2:27017/')
for doc in c1.firstdabase.somecollection.find():
      c2.seconddatabase.somecollection.insert(doc)

The code above is a rough solution. Depending on you data volume you should probably look for batching: https://pymongo.readthedocs.io/en/stable/examples/bulk.html

Mihai
  • 831
  • 6
  • 13
  • Thanks, but we are running `mongoengine` in the project. That is a set thing... – El Dude Mar 08 '21 at 20:09
  • 1
    Mongoengie uses pymongo behind the scenes. You can use your model to get the objects and use pymongo to insert in the second one for example. – Mihai Mar 08 '21 at 20:16
  • Oh, I didnt know that... Mongo newbie. Will look closer into it then. – El Dude Mar 08 '21 at 20:25
  • Here is a good example: https://stackoverflow.com/questions/12068558/use-mongoengine-and-pymongo-together – Mihai Mar 08 '21 at 21:15