[SOLVED] Reactive update of documents returns wrong/old data


I don’t think there is anything specific in the code being that it’s extremely simple, and I have found the source of the problem to be on reactive update of collection.find() which updates twice, first time data is correct, but on last update it isn’t.

Basic Specs

What I am doing is simple and straight forward:

  1. I have groups and documents.
  2. The documents are nested within groups; so documents have a groupId field that stores the group it belongs to, and an order field which stores the order.
  3. Whenever a document is moved to a new group, the order for all documents in both old/new groups is updated, and the groupId for the moved document is updated.

Simplest Reproducible Example

Here is an extremely simple example of where this fails, and the reactive data returns the incorrect values, even though it’s correct in the db!!

  1. I have two groups, and two documents
  2. Each group has one document as a child
  3. I move one document out of a group into the other, leaving one group empty, and the other with two documents… and here is what happens

Output from console.log()

collection.update() is called two times; one to update the order & new groupId of the moved item, and another to update the order of the document that already existed in the group

>>> collection.update()
{_id: "dAQQu7ChSQHSbFyd8", order: 0, groupId: "nYF2pdex62ChyAtNq"}
{_id: "fZR7TjaoRGbN8Mz5B", order: 1}

Template helper that renders the groups/documents receives reactive update and is called twice.

Here is the output from collection.find().fetch() for the first update – everything is correct up until this point.

>>> helper() called
{_id: "dAQQu7ChSQHSbFyd8", order: 0, groupId: "nYF2pdex62ChyAtNq"}
{_id: "fZR7TjaoRGbN8Mz5B", order: 1, groupId: "nYF2pdex62ChyAtNq"}

Helper receives reactive update again (after the one above), and now the groupId value of the document moved is wrong?!

>>> helper() called
{_id: "dAQQu7ChSQHSbFyd8", order: 0, groupId: "SmK8bJpgB8MQxGKWy"}
{_id: "fZR7TjaoRGbN8Mz5B", order: 1, groupId: "nYF2pdex62ChyAtNq"}

At this point there is twitch in the UI and the item is moved back to it’s original group – but the db has the correct data, and if the page is reloaded it displays properly.

Any guidance, or help would be greatly appreciated!


I’m not sure I’m following your example properly, but I wonder whether this problem is related to this post by @glasser:


@robfallows – thank you for that link, without it i wouldn’t of figured it out!

i did some queries on oplog.rs and found that everything was working just fine – so either meteor is not properly reading oplog, or it’s not even using it… turns out it wasn’t even using it, it was just polling for changes.

since i am not using the bundled mongodb, i had to set both MONGO_OPLOG_URL to the local db before starting the app.

appreciate the response!