1

I am developing a simple Social network and need help with mongorepository query. I have 2 documents:

@Document(collection = "post")
public class Post {
    ...........
    @DBRef
    @Field("settings")
    private Settings settings;

and

@Document(collection = "settings")
public class Settings {
    ..............
    @Field("privacy_settings")
    private PrivacySettings privacySettings;
}

PrivacySettings is an enum with settings ONLY_ME, EVERYONE, FRIENDS.

And the situation is: a friend is viewing on my page and he can see my posts with correct privacy settings(etc. he can see only posts with PrivacySettings: EVERYONE, FRIENDS, but not the ONLY_ME). Ideas, how to resolve this problem? How to create a correct query?

IMParasharG
  • 1,869
  • 1
  • 15
  • 26
Volodymyr Zavada
  • 579
  • 2
  • 8
  • 18

1 Answers1

3

Solution 1
In Mongo, you can't query of DbRef fields except for Id as no join is supported in this. As read operation is very high I will recommend you to embed part of Setting document in Post instead of referencing it. As mongo DB design suggest if read > write you should embed the document. In this case, the query will be simple.

Solution 2
You can fire two DB queries 1st on Setting document to get the Setting with EVERYONE, FRIENDS the make query on Post document to get all post with Setting in query.

Solution 3
You can use QueryDSL for query on DbRef object easily for reference see this.

Nishant Bhardwaz
  • 924
  • 8
  • 21