java - How to ensure execution order -
what different ways , best way ensure order of execution when different database operations involved , 1 operations depends on other operations. in code this:
string countryname = "name"; country countryobj = new country(); countryobj.setname(countryname); countryobj.setstates(new arraylist<state>()); // update database persistencemanager manager = new persistencemanager(); list<country> countries = manager.getallcountries(); if (countries != null && !countries.isempty()) { (country country2 : countries) { if (country2.getname().equalsignorecase(countryname)) { return; } } } manager.savecountry(countryobj, country);
here manager.getallcountries() retrieves countries database , manager.savecountry depends on previous operation
i think need start point of looking @ modeling data use atomic operations.
what describing insert data if not present. upsert , looking logic if country exists don't want change (as return out of method). can use $setoninsert operator - only save values of countryobj
if doesn't exist.
the last hurdle name matching - testing equivalence against lowercased versions of names - use regular expression in query full scan of documents in collection - alternative store lowercased name match against.
an example queries , inserts if not present using shell syntax below:
db.collection.update( {lname: countryname.tolowercase()}, { $setoninsert: { name: countryname, states: [{name: "victoria"}] } }, { upsert: true } )
of course if wanted update information if country
exist can use $set
operator update if country exists.
Comments
Post a Comment