0

I am using MongoDB in my web API. MongoDB is being updated/inserted by other sources.

How do I query mongodb to get only newly inserted or updated documents ?

I can get sorted documents by below query but this doesn't solve my purpose

db.collectionName.findOne({}, {sort:{$natural:-1}})

Is there any log or other way like in SQL there is INSERTED and UPDATED

Mahajan344
  • 2,492
  • 6
  • 39
  • 90

1 Answers1

0

What are these newly inserted/updated documents in your context?

Assuming that you need to fetch newly/inserted documents relative to a time, you need to have a field in your collection that holds the time stamp (for example, inserted, and lastUpdated fields), you have an operator to help with updating. But this needs application changes.

Or you can use change streams, for a trigger like functionality. You can listen for changes and take actions as changes are made.

Andrew Nessin
  • 1,206
  • 2
  • 15
  • 22