List of usage examples for com.mongodb DBCollection remove
public WriteResult remove(final DBObject query)
From source file:org.codinjutsu.tools.mongo.logic.MongoManager.java
License:Apache License
public void delete(ServerConfiguration configuration, MongoCollection mongoCollection, Object _id) { MongoClient mongo = null;// ww w .j a va 2 s .com try { String databaseName = mongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); DB database = mongo.getDB(databaseName); DBCollection collection = database.getCollection(mongoCollection.getName()); collection.remove(new BasicDBObject("_id", _id)); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.codinjutsu.tools.nosql.mongo.logic.MongoClient.java
License:Apache License
public void delete(ServerConfiguration configuration, MongoCollection mongoCollection, Object _id) { com.mongodb.MongoClient mongo = null; try {/* w w w.j av a 2 s. co m*/ String databaseName = mongoCollection.getDatabaseName(); mongo = createMongoClient(configuration); DB database = mongo.getDB(databaseName); DBCollection collection = database.getCollection(mongoCollection.getName()); collection.remove(new BasicDBObject("_id", _id)); } catch (UnknownHostException ex) { throw new ConfigurationException(ex); } finally { if (mongo != null) { mongo.close(); } } }
From source file:org.cvbase.service.mongodb.MongodbService.java
License:Open Source License
/** * {@inheritDoc}/*from w w w. j a v a2 s.c o m*/ * * This is an implementation for Mongo DB. */ @Override public <T extends Model> boolean delete(T t) { boolean status = false; if (t == null || find(t.getClass(), t.getOid()) == null) { return status; } Class<?> type = t.getClass(); DBCollection coll = db.getCollection(type.getSimpleName()); BasicDBObject object = new BasicDBObject(); object.put(Model.OID, t.getOid()); try { LOG.log(Level.INFO, "{0}", t); WriteResult writeResult = coll.remove(object); if (writeResult.getError() != null) { LOG.log(Level.WARNING, "Delete of {0} is failed.", t); } else { status = true; } } catch (MongoException e) { LOG.log(Level.SEVERE, "Delete of " + t + " is failed.", e); } return status; }
From source file:org.datanucleus.store.mongodb.MongoDBPersistenceHandler.java
License:Open Source License
public void deleteObject(ObjectProvider op) { // Check if read-only so update not permitted assertReadOnlyForUpdateOfObject(op); AbstractClassMetaData cmd = op.getClassMetaData(); ExecutionContext ec = op.getExecutionContext(); ManagedConnection mconn = storeMgr.getConnection(ec); try {// ww w .j a v a 2 s. co m DB db = (DB) mconn.getConnection(); long startTime = System.currentTimeMillis(); if (NucleusLogger.DATASTORE_PERSIST.isDebugEnabled()) { NucleusLogger.DATASTORE_PERSIST.debug(LOCALISER_MONGODB.msg("MongoDB.Delete.Start", op.getObjectAsPrintable(), op.getInternalObjectId())); } DBCollection collection = db.getCollection(storeMgr.getNamingFactory().getTableName(cmd)); DBObject dbObject = MongoDBUtils.getObjectForObjectProvider(collection, op, true, false); if (dbObject == null) { if (cmd.isVersioned()) { throw new NucleusOptimisticException("Object with id " + op.getInternalObjectId() + " and version " + op.getTransactionalVersion() + " no longer present"); } else { throw new NucleusDataStoreException( "Could not find object with id " + op.getInternalObjectId()); } } // Save the dbObject in case we need to load fields during the deletion op.setAssociatedValue(OP_DB_OBJECT, dbObject); // Invoke any cascade deletion op.loadUnloadedFields(); op.provideFields(cmd.getAllMemberPositions(), new DeleteFieldManager(op, true)); // Delete this object op.removeAssociatedValue(OP_DB_OBJECT); if (NucleusLogger.DATASTORE_NATIVE.isDebugEnabled()) { NucleusLogger.DATASTORE_NATIVE.debug("Removing object " + op + " as " + dbObject); } collection.remove(dbObject); if (ec.getStatistics() != null) { ec.getStatistics().incrementNumWrites(); ec.getStatistics().incrementDeleteCount(); } if (NucleusLogger.DATASTORE_PERSIST.isDebugEnabled()) { NucleusLogger.DATASTORE_PERSIST.debug( LOCALISER_MONGODB.msg("MongoDB.ExecutionTime", (System.currentTimeMillis() - startTime))); } } catch (MongoException me) { NucleusLogger.PERSISTENCE.error("Exception deleting object " + op, me); throw new NucleusDataStoreException("Exception deleting object for " + op, me); } finally { mconn.release(); } }
From source file:org.datavyu.models.db.MongoDatastore.java
License:Open Source License
public MongoDatastore() { if (!running) { this.startMongo(); }// w ww .j av a 2s . c o m // Clear documents if any. DBCollection varCollection = mongoDB.getCollection("variables"); DBCursor varCursor = varCollection.find(); while (varCursor.hasNext()) { varCollection.remove(varCursor.next()); } DBCollection cellCollection = mongoDB.getCollection("cells"); DBCursor cellCursor = cellCollection.find(); while (cellCursor.hasNext()) { cellCollection.remove(cellCursor.next()); } // Place indexes on the cell collection for fast querying BasicDBObject cell_index = new BasicDBObject(); cell_index.put("variable_id", 1); cell_index.put("onset", 1); cellCollection.ensureIndex(cell_index); cellCollection.ensureIndex(new BasicDBObject("variable_id", 1)); cellCollection.ensureIndex(new BasicDBObject("onset", 1)); cellCollection.ensureIndex(new BasicDBObject("offset", 1)); cell_index = new BasicDBObject(); cell_index.put("onset", 1); cell_index.put("offset", 1); cellCollection.ensureIndex(cell_index); DBCollection matrixCollection = mongoDB.getCollection("matrix_values"); DBCursor matrixCursor = matrixCollection.find(); while (matrixCursor.hasNext()) { matrixCollection.remove(matrixCursor.next()); } matrixCollection.ensureIndex(new BasicDBObject("parent_id", 1)); DBCollection nominalCollection = mongoDB.getCollection("nominal_values"); DBCursor nominalCursor = nominalCollection.find(); while (nominalCursor.hasNext()) { nominalCollection.remove(nominalCursor.next()); } nominalCollection.ensureIndex(new BasicDBObject("parent_id", 1)); DBCollection textCollection = mongoDB.getCollection("text_values"); DBCursor textCursor = textCollection.find(); while (textCursor.hasNext()) { textCollection.remove(textCursor.next()); } textCollection.ensureIndex(new BasicDBObject("parent_id", 1)); // Clear variable listeners. MongoVariable.clearListeners(); MongoDatastore.changed = false; }
From source file:org.datavyu.models.db.MongoDatastore.java
License:Open Source License
@Override public void removeVariable(final Variable var) { DBCollection varCollection = mongoDB.getCollection("variables"); BasicDBObject query = new BasicDBObject(); for (Cell c : var.getCells()) { var.removeCell(c); }/*from w w w . ja v a 2 s . c om*/ for (DatastoreListener dbl : this.dbListeners) { dbl.variableRemoved(var); } query.put("_id", ((MongoVariable) var).getID()); varCollection.remove(query); markDBAsChanged(); }
From source file:org.datavyu.models.db.MongoVariable.java
License:Open Source License
@Override public void removeCell(final Cell cell) { DBCollection cell_collection = MongoDatastore.getDB().getCollection("cells"); // Confirm we have the right cell BasicDBObject query = new BasicDBObject(); query.put("_id", ((MongoCell) cell).getID()); DBCursor cur = cell_collection.find(query); if (!cur.hasNext()) { System.err.println("ERROR: Cell not found. Nothing deleted."); } else {//from w ww .j av a 2s . co m cell_collection.remove(cur.next()); } MongoDatastore.markDBAsChanged(); for (VariableListener vl : getListeners(getID())) { vl.cellRemoved(cell); } }
From source file:org.eclipselabs.restlet.mongo.MongoResource.java
License:Open Source License
@Delete public void remove() throws UnknownHostException { DBCollection collection = getCollection(); collection.remove(new BasicDBObject("_id", new ObjectId((String) getRequestAttributes().get("id")))); }
From source file:org.einherjer.week2.samples.UpdateRemoveSample.java
License:Apache License
private static void scratch(DBCollection collection) { //whole document replacement, remember the _id field is not replaces so in this case, since _id was the only field, age gets added // if we do this a second time with gender:"F" instead of age:24 the document would end up with _id and gender fields, and age would be gone collection.update(new BasicDBObject("_id", "alice"), new BasicDBObject("age", 24)); //sets adds the gender field to the existing _id and age fields collection.update(new BasicDBObject("_id", "alice"), new BasicDBObject("$set", new BasicDBObject("gender", "F"))); //upsert ("frank" didn't exist originally, so this update actually performs an insert) collection.update(new BasicDBObject("_id", "frank"), new BasicDBObject("$set", new BasicDBObject("age", 24)), true, false); //multi document update collection.update(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject("title", "Dr")), false, true);//from ww w .j a v a2s .co m collection.remove(new BasicDBObject("_id", "frank")); }
From source file:org.exist.mongodb.xquery.mongodb.collection.Remove.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {//from w w w. j a va 2 s . 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; // Get collection in database DB db = client.getDB(dbname); DBCollection dbcol = db.getCollection(collection); // Execute query WriteResult result = dbcol.remove(query); return new StringValue(result.toString()); } 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()); } }