Reputation: 867
I’m new to MongoDB and Mongojack , I’m trying to update a document if it is already exit or else insert a new document. For testing purpose I use below code which is button click event. My problem is when I click the button for the first time it adds a document with generated object id but when I click again, it adds another document with the same object id. I ‘m expect to keep only one document in this collection, by updating each time.
after googling I added @objectId
annotation to the _id
field, but gives me an error with the below root couse;
any help ..
Model class
public class Transport implements StockLoadable {
// @ObjectId
private String _id;
private String info;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
@Override
public String toString() {
return "Vehicle";
}
@Override
public void setStock(Stock stock) {
// TODO Auto-generated method stub
}
@Override
public Stock getStock() {
// TODO Auto-generated method stub
return null;
}
}
Button Click event
Transport transport;
btn.addClickListener(o->{
if(transport==null){
transport= new Transport();
transport.setInfo("Info");
}
try (MongoClient mongoClient = new MongoClient("localhost",27017)) {
DB db = mongoClient.getDB("transport");
DBCollection table = db.getCollection("test");
JacksonDBCollection<Transport, String> collection = JacksonDBCollection.wrap(table, Transport.class, String.class);
WriteResult<Transport, String> result = collection.save(transport);
System.out.println(result.getSavedId());
transport= collection.findOne(DBQuery.is("_id",result.getSavedId()));
} catch (Exception e) {
e.printStackTrace();
}
});
Root Exception i'm having after adding annotation
Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.introspect.Annotated.getType()Lcom/fasterxml/jackson/databind/JavaType;
at org.mongojack.internal.MongoAnnotationIntrospector.findDeserializer(MongoAnnotationIntrospector.java:86)
at com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair.findDeserializer(AnnotationIntrospectorPair.java:515)
at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findDeserializerFromAnnotation(BasicDeserializerFactory.java:1648)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.constructSettableProperty(BeanDeserializerFactory.java:721)
at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:498)
Upvotes: 0
Views: 695
Reputation: 867
I’m answering my own question, which may be useful for others. The reason for duplicating document is my misunderstanding of mongodb automatic id process.
If you need to let the db to issue an id for your document, then you have to use org.bson.types.ObjectId
type of field with the name of _id
and leave it empty.
If you want to add an id as your choice, then define a field with type you want with the name of _id
and assign appropriate value for it. If you leave it empty, mongo will add an id of type ObjectId
which will cause consequent problems as I had.
Change the type of _id
field from String
to ObjectId
solved by problem. But I was unable to find the reason for the exception. Just removed the annotations too.
Upvotes: 1