0

Is there a way to share documents between partitions to avoid duplication?

If I have enum data Message Status (sent, sending, failed, undeliverable, etc.), I have to make a copy for each partition, instead of having all share the same statuses.

Example for clarity:
User A has access to partition 1, user B has access to partition 2. For user A to have access to the enum data, it needs to exist with partition key 1. Same thing with user B, he needs a copy of the enum documents with partition key 2, because he cannot access the existing one with partition 1. So the data ends up being duplicated.

Buzzkill
  • 68
  • 2
  • 8
  • What do you mean by "partition"? It does not exist in MongoDB, it comes from relational RDMBS. – Wernfried Domscheit Mar 21 '21 at 22:01
  • https://docs.mongodb.com/realm/sync/partitioning/ – Buzzkill Mar 22 '21 at 06:32
  • Sorry I never worked with MongoDB Atlas. Anyway, I don't understand your question, what is your problem? How does your document look like and what is the partition key? – Wernfried Domscheit Mar 22 '21 at 07:29
  • My partition key is a location Id, users have access to different locations. Problem is that, I have to duplicate the message statuses (and pretty much any `enum`-like data) for each location Id (partition). Here's an example to make it more clear - location `1` & `2`, I would need 2 copies of `sent` message status, one with partition key `1` and the other one with `2`. If I have it only for one location, the other ones won't be able to access that message status. – Buzzkill Mar 22 '21 at 09:20
  • Instead of describing your issue, please update the question with code demonstrating the issue. Also, it's unclear why you're duplicating data or unable to access data in multiple partitions ; that's a common task. Please update and clarify the question and take a moment and please take a moment and review [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Mar 22 '21 at 17:54

1 Answers1

0

Got an answer from another place:

"No, at this time a Realm Object (that translates into a document when stored in MongoDB) can only belong to a single partition: if you need to refer to the same piece of data from multiple partitions, two of the pattern you can follow:

  • If the data is small, you can use an EmbeddedObject (or a list of them), that, while using more space because of duplicates, allows a quicker access to the data itself without a roundtrip to the DB. If you're talking about enums, this looks like a good choice: an ObjectId would take some amount of data anyway.

  • Define another partition with the common data, and open a separate, specific realm on the client for it: you'll have to manually handle the relationships, though, i.e. you can store the ObjectIds in your main realm, but will need to query the secondary one for the actual objects. Probably worth only if the common data is complex enough to justify the overhead."

Buzzkill
  • 68
  • 2
  • 8
  • 1
    This answer doesn't seem to have anything to do with the question. A *partition* is not a separation of data. Objects with different partitions exist within the same Atlas collection so there is no concept of 'sharing between a partition; a partition can be accessed at any time. For example; assume messages have a _partitionKey property 'message'. User A needs to update a message status so they connect to Realm with partition 'message' and then update the message status. Then User B needs to update that same message so they connect to the message Realm and update the message. Make sense? – Jay Mar 23 '21 at 18:12
  • Hopefully this is more clear - user A has access to partition `1`, user B has access to partition `2`. Now imagine that you have any enum data, like the mentioned message status. For user A to have access to that enum data, it needs to exist with partition key `1`. Same thing with user B, he needs a copy of the enum document with partition key `2`, because he cannot access the existing one with partition `1`. Thus it ends up duplicated for different partitions. – Buzzkill Mar 23 '21 at 22:43
  • I understand what you're saying in your comment but that's not accurate. Any user can access any partition at any time, provided security allows for it. While there is a [Realm enum](https://docs.mongodb.com/realm/sdk/ios/examples/define-a-realm-object-model/#declare-enum-properties), which is backed by an Int, it's actually just a raw value. So user A and user B can both write enum data to their partitions (1, 2, 3 etc) at any time, without constraint. Also any user can or access any other partition as needed - there's no requirement to only access one partition. – Jay Mar 24 '21 at 13:28