Reputation: 996
Have a nice day everyone!
I'm trying to understand MongoDB in the JAVA. I'm trying to map the MongoDB Document object to my own java object.
My MongoDB Document structure:
{
"_id" : {
"$oid" : "5f2d37f1cdf2f93d01fd5f9a"
},
"Person_ID" : {
"$numberInt" : "3"
}, "Name" : "John", "Lastname" : "Doe"
}
MyClass.class model:
public class MyClass {
String oid;
int Person_ID;
int numberInt;
String Name, Lastname;
//empty constructor
public MyClass() {}
// Setters and Getters
}
Using JAVA I try:
public static void main(String[] args) {
MongoClientURI uri = new MongoClientURI(
"mongodb+srv://<username>:<password>@cluster.lt8te.mongodb.net/dbProject?
retryWrites=true&w=majority");
MongoClient client = new MongoClient(uri);
MongoDatabase db = client.getDatabase("dbProject");
MongoCollection<Document> coll = db.getCollection("myCollection");
Document doc = (Document) coll.find().first();
System.out.println(doc.toJson());
Gson gson = new Gson();
MongoObject mongoObj = gson.fromJson(doc.toJson(), MyClass.class);
}
I'm getting an error: Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 10 path $._id
I think my MyClass model not matches the Document mongoDB model. I'm not very sure where I have a mistake. Or what to edit? Thank You.
Upvotes: 1
Views: 18573
Reputation: 996
So after a few hours of studying. And with the help of StackOverFlow, I can present a solution here that can help others. Let's grasp the structure of the BSON Document, which I gave here as an example.
Here:
{
"_id" : {
"$oid" : "5f2d37f1cdf2f93d01fd5f9a"
},
"Person_ID" : {
"$numberInt" : "3"
}, "Name" : "John", "Lastname" : "Doe"
}
MongoDB requires that you have an '_id' field for all documents. If you don't provide one the driver will assign a ObjectId with a generated value.
Now you need to create an object model of this JSON. Like this: (It is important to maintain the order of the fields.)
import org.bson.types.ObjectId;
public class ModelMongo
{
ObjectId _id;
int Person_ID;
String Name;
String Lastname;
public ModelMongo(ObjectId id, int Person_ID, String Name, String Lastname)
}
this._id = id;
this.Person_ID = Person_ID;
this.Name = Name;
this.Lastname = Lastname;
{
// Setter and Getter ...
}
Assuming you have the same document structure as in the example above and you want to map a domain mongo document to your own java class:
public static void main(String[] args)
}
MongoClient client = new MongoClient(uri);
MongoDatabase db = client.getDatabase("dbProject");
MongoCollection<org.bson.Document> coll = db.getCollection("myCollection");
Document doc = (Document) coll.find().first();
Gson gson = new Gson();
ModelMongo model = gson.fromJson(doc.toJson(), ModelMongo.class);
/*** ****
* Now in the variable model is a Document from MongoDB *
* Access to variables of that Object possible through *
* its Getter methods. ****/
{
Dependency:
<!-- https://mvnrepository.com/artifact/org.mongodb/bson -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>4.0.4</version>
</dependency>
Upvotes: 4
Reputation: 1038
At last line you are trying to map a JSON to your MyClass java object. For that to work JSON structure has to match with your java class. Assuming the JSON result from doc.toJson()
will look like your Mongo document that you have shown, implement your class as below. The idea is your JSON documents data type should match with your class, For example Person_ID
in your JSON is an Object which has three attribute in it so in your java class there should be a variable with name Person_ID which will of a another Class type with three attribute.
public class MyClass {
ID _id;
Person Person_ID;
//empty constructor
public MyClass() {}
// Setters and Getters
}
Class ID{
public String oid;
//Getter setter
}
Class Person {
int numberInt;
String Name;
String Lastname;
//Getter setter
}
Upvotes: 1