Morphia and MongoDB - Modeling Something like settings

I am currently evaluating MongoDB and Morphia. How would I simulate something like “settings”, where there is only one “record” (I'm not sure about the correct Mongolian term). Should I override the save method in the object class? An example of how to do this and how to use it will be amazing.

For example, I would like to save the homepage configuration:

home page settings
  show friends list:  false
  marketing text:  "You'll love it here"
  main image:  main.jpg
+3
source share
1 answer

If you need only one copy of the settings for your application (for example, singleton), I would suggest something like this:

@Entity
class Settings {
  @Id int id = 0;
  boolean showFriendsList = false;
  String marketingText = "You'll love it";
  byte[] mainImage = ...; 
}

id , . insert, , ( ).

, get/change/save update.

Datastore ds = ...;

//get/change/save
Settings s = ds.find(Settings.class).get(); //like findOne in the shell/driver
s.showFriendsList = true;
ds.save(s); 

//or update
ds.updateFirst(ds.find(Settings.class), ds.creatUpdateOperations(Settings.class).set("showFiendsList", true));
+8

Source: https://habr.com/ru/post/1773054/


All Articles