Mongosphere is a Java library wrapped around MongoDB Driver Sync with the aim of speeding up development overall and also reduce overall lines of code written with the assistance of Reflection.
You can easily install this library from Central Maven:
<dependency>
<groupId>pw.mihou</groupId>
<artifactId>Mongosphere</artifactId>
<version>1.0.3</version>
</dependency>
implementation 'pw.mihou:Mongosphere:1.0.3'
Please read our wiki for a more detailed explanation of many of the things here from creating the models to inserting a model and fetching a model from the database, here is a little table of contents in proper order of what you should read first.
- Installation
- Annotations
- Creating Models
- Creating Mongosphere
- Type Adapters
- Inserting Models with Mongosphere
- Fetching Models with Mongosphere
- Example Usage
A very simple example of Mongosphere usage is can be seen below, please read our wiki for a more detailed introduction over Mongosphere like what to do if Mongosphere cannot detect a constructor or cannot parse a type, etc.
import pw.mihou.mongosphere.annotations.MongoConstructor;
import pw.mihou.mongosphere.annotations.MongoItem;
public class TestModel {
private String id;
@MongoItem(key = "content")
private String someContent;
@MongoConstructor
public TestModel(@MongoItem(key = "id") String id, @MongoItem(key = "content") String someContent) {
this.id = id;
this.someContent = someContent;
}
public String getId() {
return id;
}
public String getContent() {
return someContent;
}
}
import com.mongodb.ConnectionString;
import pw.mihou.mongosphere.Mongosphere;
import pw.mihou.mongosphere.core.MongoBase;
public class Test {
private static MongoBase base;
public static void main(String[] args) {
// Initialize Mongosphere.
base = Mongosphere.create(new ConnectionString(System.getenv("mongodb")));;
}
/**
* This retrieves a model that has the id of hello on MongoDB and turn
* the returned data into the TestModel.
*
* @return The test model returned.
*/
public TestModel getExample() {
return base.fromDatabase("databaseName")
.fromCollection("collectionName")
.findWhere(TestModel.class, "id", "ID"); // the second ID refers to the value below.
}
/**
* This inserts a model into the database using Mongosphere.
*/
public void insertExample() {
TestModel model = new TestModel("ID", "CONTENT");
// If you want to automatically replace an existing one with the newer model.
base.fromDatabase("databaseName")
.fromCollection("collectionName")
.insertOrReplace(model, "id");
// If you want to insert the model even if it means duplicating it.
base.fromDatabase("databaseName")
.fromCollection("collectionName")
.insert(model);
}
}