Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (4k points)

The mongo find(), return all the documents in a collection. I want to store every document that I have in my MongoDB into another database. So, I want to keep polling MongoDB for a single new document, because find().limit(1) would keep returning me the same again and again. I just want mongo to return, all the documents one by one. Is it possible? How can this be done?

Edit: I want to just copy every entry in my MongoDB instance to another DB, not just the latest ones.

1 Answer

0 votes
by (8.7k points)

This can be done using two different methods below:

  • You can do it with the help of sort and by default, you find it  as natural sort:

find().sort({_id:-1}).limit(1) 

  • And the better way is to use the tailable cursor.

To learn more about it, check out the MongoDB tutorial by Intellipaat with 24X7 support. 

Browse Categories

...