List of usage examples for com.mongodb DBCollection findAndModify
public DBObject findAndModify(final DBObject query, final DBCollectionFindAndModifyOptions options)
From source file:com.sube.daos.mongodb.EntryDaoImpl.java
License:Apache License
private void markAs(DataEntry dataEntry, String status) { DBObject query = getQuery(dataEntry); DBCollection collection = getCollection(); DBObject update = dataEntryGenerator.generate(dataEntry); update.put("status", status); collection.findAndModify(query, update); }
From source file:com.sube.daos.mongodb.GenericProviderDaoImpl.java
License:Apache License
private void markAs(Provider provider, String status) { DBCollection collection = getProvidersCollection(); BasicDBObject query = getProviderQuery(provider); BasicDBObject update = new BasicDBObject(); update.put("status", status); collection.findAndModify(query, update); }
From source file:de.belaso.mongolyn.ui.TaskDataHandler.java
License:Open Source License
@Override public RepositoryResponse postTaskData(TaskRepository repository, TaskData taskData, Set<TaskAttribute> oldAttributes, IProgressMonitor monitor) throws CoreException { DBCollection dbCollection = MongolynUtils.getDBCollection(repository); BasicDBObjectBuilder bob = BasicDBObjectBuilder.start(); for (Map.Entry<String, TaskAttribute> entry : taskData.getRoot().getAttributes().entrySet()) { String key = entry.getKey(); TaskAttribute attribute = entry.getValue(); String attributeValue = attribute.getValue(); if (attributeValue != null) { bob.add(key.replace('.', '_'), attributeValue); }// www. j ava 2 s. co m } DBObject dbObject = bob.get(); try { if (taskData.isNew()) { dbCollection.insert(dbObject, WriteConcern.SAFE); return new RepositoryResponse(ResponseKind.TASK_CREATED, dbObject.get("_id").toString()); } else { dbCollection.findAndModify(new BasicDBObject("_id", new ObjectId(taskData.getTaskId())), dbObject); return new RepositoryResponse(ResponseKind.TASK_UPDATED, taskData.getTaskId()); } } catch (MongoException mongoException) { throw new CoreException(Activator.INSTANCE.getErrorStatus(mongoException)); } }
From source file:it.wami.map.mongodeploy.OsmSaxHandler.java
License:Apache License
/** * Upsert method//from w w w .j a v a2 s .c o m * @param obj the entry * @param collection */ private void upsert(DBObject obj, String collection) { DBCollection coll = db.getCollection(collection); coll.findAndModify(new BasicDBObject("_id", obj.get("_id")), obj); }
From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java
public void setParachutistCounter(int number) { DBCollection counterDbCollection = db.getCollection("counters"); BasicDBObject query = new BasicDBObject("_id", "jumpers"); BasicDBObject increment = new BasicDBObject().append("seq", number); counterDbCollection.findAndModify(query, increment); }
From source file:no.nlf.avvik.melwinSOAPconnection.MongoOperations.java
public int getParachutistCounter() { DBCollection counterDbCollection = db.getCollection("counters"); BasicDBObject query = new BasicDBObject("_id", "jumpers"); BasicDBObject increment = new BasicDBObject().append("$inc", new BasicDBObject().append("seq", 1)); BasicDBObject counterDBObject = (BasicDBObject) counterDbCollection.findAndModify(query, increment); int seq = (int) counterDBObject.getInt("seq"); return seq;/*from www . j a v a 2s . c o m*/ }
From source file:org.eclipselabs.restlet.mongo.MongoResource.java
License:Open Source License
@Put public void update(JsonRepresentation representation) throws JSONException, UnknownHostException { JSONObject json = representation.getJsonObject(); DBCollection collection = getCollection(); DBObject dbObject = (DBObject) JSON.parse(json.toString()); if (collection.findAndModify( new BasicDBObject("_id", new ObjectId((String) getRequestAttributes().get("id"))), dbObject) == null)/* w ww . j av a 2s. c o m*/ getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); }
From source file:org.exist.mongodb.xquery.mongodb.collection.FindAndModify.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {/* w ww. j ava2s . c o m*/ // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String collection = args[2].itemAt(0).getStringValue(); BasicDBObject query = (args.length >= 4) ? (BasicDBObject) JSON.parse(args[3].itemAt(0).getStringValue()) : null; BasicDBObject update = (args.length >= 5) ? (BasicDBObject) JSON.parse(args[4].itemAt(0).getStringValue()) : null; BasicDBObject sort = (args.length >= 6) ? (BasicDBObject) JSON.parse(args[5].itemAt(0).getStringValue()) : null; // Map<String,Boolean> options = (args.length >= 7) // ? convertOptions((AbstractMapType) args[6].itemAt(0)) // : null; // Get database DB db = client.getDB(dbname); DBCollection dbcol = db.getCollection(collection); //query update sort DBObject result; if (sort == null /* && options==null */) { result = dbcol.findAndModify(query, update); } else /* if (options==null) */ { result = dbcol.findAndModify(query, sort, update); } // else { // options.putIfAbsent("remove", unordered); // options.putIfAbsent("returnNew", unordered); // options.putIfAbsent("update", unordered); // options.putIfAbsent("upsert", unordered); // // dbcol.findAndModify(query, sort, update, unordered, result, unordered, unordered); // } // Execute query Sequence retVal = (result == null) ? Sequence.EMPTY_SEQUENCE : new StringValue(result.toString()); return retVal; } catch (JSONParseException ex) { LOG.error(ex.getMessage()); throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage()); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage()); } }
From source file:v7db.files.mongodb.Vermongo.java
License:Open Source License
/** * updates an existing object. The object must have been the _version * property set to the version number of the base revision (i.e. the version * number to be replaced). If the current version in the DB does not have a * matching version number, the operation aborts with an * UpdateConflictException.//w w w. j a va 2 s .co m * * After the update is successful, _version in the object is updated to the * new version number. * * The version that was replaced is moved into the collection's shadow * collection. * * @param collection * @param object * @throws UpdateConflictException */ static void update(DBCollection collection, DBObject object) throws UpdateConflictException { if (!object.containsField(_VERSION)) throw new IllegalArgumentException("the base version number needs to be included as _version"); int baseVersion = (Integer) object.get(_VERSION); // load the base version { DBObject base = collection.findOne(new BasicDBObject("_id", getId(object))); if (base == null) { throw new IllegalArgumentException("document to update not found in collection"); } Object bV = base.get(_VERSION); if (bV instanceof Integer) { if (baseVersion != (Integer) bV) { throw new UpdateConflictException(object, base); } } else { throw new UpdateConflictException(object, base); } // copy to shadow DBCollection shadow = getShadowCollection(collection); base.put("_id", new BasicDBObject("_id", getId(base)).append(_VERSION, baseVersion)); WriteResult r = shadow.insert(base, WriteConcern.SAFE); // TODO: if already there, no error r.getLastError().throwOnError(); } try { object.put(_VERSION, baseVersion + 1); DBObject found = collection .findAndModify(new BasicDBObject("_id", getId(object)).append(_VERSION, baseVersion), object); if (found == null) { // document has changed in the mean-time. get the latest version // again DBObject base = collection.findOne(new BasicDBObject("_id", getId(object))); if (base == null) { throw new IllegalArgumentException("document to update not found in collection"); } throw new UpdateConflictException(object, base); } } catch (RuntimeException e) { object.put(_VERSION, baseVersion); throw e; } }