java - How to be avoid duplicates entries at insert to MongoDB -
i want shure when inputting new dbobject db unique , collection doesn't contain key field duplicates .
here how looks now:
public abstract class abstractmongodao<id, model> implements genericdao<id, model> { protected mongo client; protected class<model> model; protected dbcollection dbcollection; /** * contains model data : unique key name , name of method */ protected keyfield keyfield; @suppresswarnings("unchecked") protected abstractmongodao() { parameterizedtype genericsuperclass = (parameterizedtype) this.getclass().getgenericsuperclass(); model = (class<model>) genericsuperclass.getactualtypearguments()[1]; getkeyfield(); } public void connect() throws unknownhostexception { client = new mongoclient(config.getmongohost(), integer.parseint(config.getmongoport())); db clientdb = client.getdb(config.getmongodb()); clientdb.authenticate(config.getmongodbuser(), config.getmongodbpass().tochararray()); dbcollection = clientdb.getcollection(getcollectionname(model)); } public void disconnect() { if (client != null) { client.close(); } } @override public void create(model model) { object keyvalue = get(model); try { objectmapper mapper = new objectmapper(); string requestasstring = mapper.writerwithdefaultprettyprinter().writevalueasstring(model); // check if not presented basicdbobject dbobject = new basicdbobject((string) keyvalue, requestasstring); dbcollection.ensureindex(dbobject, new basicdbobject("unique", true)); dbcollection.insert(new basicdbobject((string) keyvalue, requestasstring)); } catch (throwable e) { throw new runtimeexception(string.format("duplicate parameters '%s' : '%s'", keyfield.id(), keyvalue)); } } private object get(model model) { object result = null; try { method m = this.model.getmethod(this.keyfield.get()); result = m.invoke(model); } catch (exception e) { throw new runtimeexception(string.format("couldn't find method name '%s' @ class '%s'", this.keyfield.get(), this.model.getname())); } return result; } /** * extract name of collection specified @ '@entity' annotation. * * @param clazz model class object. * @return name of collection specified. */ private string getcollectionname(class<model> clazz) { entity entity = clazz.getannotation(entity.class); string tablename = entity.value(); if (tablename.equals(mapper.ignored_fieldname)) { // think usual logger tablename = clazz.getname(); } return tablename; } private void getkeyfield() { (field field : this.model.getdeclaredfields()) { if (field.isannotationpresent(keyfield.class)) { keyfield = field.getannotation(keyfield.class); break; } } if (keyfield == null) { throw new runtimeexception(string.format("couldn't find key field @ class : '%s'", model.getname())); } }
keyfeld custom annotation:
@retention(retentionpolicy.runtime) @target(elementtype.field) public @interface keyfield { string id(); string get(); string statusprop() default "all";
but i'm not shure solution prove this. i'm newly @ mongo.
any suggestions?
a uniqueness can maintained in monbodb using _id
field. if not provide value of field, mongodb automatically creates unique id particuler collection.
so, in case create property called _id
in java & assign unique field value here. if duplicated, throw exception.
Comments
Post a Comment