When using ChangeStreams, is there a way to observe for an insertion to an array?

Hello,
I am having a little issue right now. Is there a way to observe for new Objects that are added to an Array. For example: I have a collection called all_users where every document holds info about one user and in every document there is a field called friends which is an Array. Now, that array stores Objects that are in a format {“userId” : ObjectId(“some_id”), “message” : “Some message”, “time” : “23746328764”}.
When I make a ChangeStream Pipeline and point it to the specific user/document, on every insertion or update, I get the whole document. Is there a way to get only the Object that was inserted to friends Array? I have tried without setting fullDocument: to ‘updateLookup’, but then It doesn’t give me any response.

Also a side question, can you have for example 10.000 to 50.000 ChangeStreams open since my startup will have 10.000+ users in a few weeks?

Thank You!

I do not know if there is a limit but probably it does. But I would be worry if I needed that many change stream. Is there an anti-pattern about that? I would try to do that with few streams, especially if the logic is the same.

Hi @Bilal_Drndo , and welcome to the Community Forums!

For any change events where the operationType is update , it should contains a sub-document called updateDescription describing the fields that were updated or removed by the update operation. See also Update Event.

Depending on the use case, you could just have one ChangeStream open. The process then dispatch (i.e. fork) a process for another operation. Having multiple ChangeStream opens mean that you need to avoid multiple events being processed together.

Having said all the above, depending on the use case, you could just call an extra function call in function that performs the insert/update operation without having ChangeStream.

Regards,
Wan.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.