Missing documents after Sync restart. What could have caused that?

Hello,

Here is what happened.
1- I removed a property in model A.
2 - I Added a property in model B.
2- I updated the schema in Realm. Then I got a warning about Sync. So I terminate sync et restart it.

Now when I relaunch my app I am getting only 3 documents instead of 9.
What could cause this?

All my data are still in Atlas, it seems that is a Sync problem. No logs.
I am using latest Swift SDK.

Thanks for your input.

1 Like

Hey @Thierry_Bucco - these types of issues can be pretty confusing to solve, so we’ve recently rewritten the Update Your Schema page in the Realm Sync documentation to make it easier to understand what happens with each type of schema change. Based on what you described, here’s what might have happened:

  • Removing a property in model A is considered a breaking change when using Sync. This requires you to terminate and re-enable Sync, and can result in data loss on the client side for any objects that had updates but were not synced. This is why you had to terminate and restart Sync.
  • Adding a property in model B is not a breaking change - BUT if it is a new required property, there are some consequences. If you do not update existing documents with your new property, they won’t sync to the client. This can make it look like there is data loss, even though the documents are still in Atlas. If you add the new property in Model B to your existing documents, you should then see the documents you expect to see in the client.

Hope this helps!

1 Like

Hey @Dachary_Carey, Thanks for your answer.
I added the required property and missing documents magically appeared!
Is there a way to add a property in all documents at once from the website?

Great, @Thierry_Bucco - I’m glad that worked!

You could use aggregation to add the new property to all of your existing documents. I think this is a good case for $addFields, which you can read more about in the MongoDB Manual.

If you go to Collections in the MongoDB Atlas website UI and select a collection, you’ll see an Aggregation tab above the filter bar when viewing documents in the collection. You could create an $addFields aggregation to handle this from that part of the website. If you’re doing a more complex data transformation, you might prefer to do it in MongoDB Compass, which is a nice GUI tool for connecting to your DB and interacting with the data. The nice thing about using Compass is that you don’t have to worry about accidentally hitting the back button in your browser and losing the agg pipeline you’re working on (although maybe the website would save it in that case - I haven’t tried it). It also lets you save aggregation pipelines to use again later, and it gives you the command line commands for the agg pipeline you’re building in the GUI, so if you want to see how to do things directly from the command line, it’s a nice way to learn.

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