List of usage examples for com.mongodb BasicDBObject putAll
@SuppressWarnings("unchecked") @Override public void putAll(final Map m)
From source file:net.ymate.platform.persistence.mongodb.expression.ProjectionExp.java
License:Apache License
public static ProjectionExp elemMatch(Query... queries) { ProjectionExp _exp = new ProjectionExp(); BasicDBObject _dbObj = new BasicDBObject(); for (Query _query : queries) { _dbObj.putAll((BSONObject) _query.toBson()); }// w ww . ja v a 2 s . c om _exp.__doAddOperator(IMongo.OPT.ELEM_MATCH, _dbObj); return _exp; }
From source file:net.ymate.platform.persistence.mongodb.support.Aggregation.java
License:Apache License
public Aggregation group(Operator id, Query... queries) { BasicDBObject _dbObj = new BasicDBObject(IMongo.OPT.ID, id == null ? null : id.toBson()); for (Query _query : queries) { _dbObj.putAll((BSONObject) _query.toBson()); }//from w ww . j a v a2s .c o m return group(_dbObj); }
From source file:net.ymate.platform.persistence.mongodb.support.Operator.java
License:Apache License
public Operator elemMatch(Operator... operators) { BasicDBObject _dbObj = new BasicDBObject(); for (Operator _opt : operators) { _dbObj.putAll((BSONObject) _opt.toBson()); }//from w w w . j av a2 s . c om __doAddOperator(IMongo.OPT.ELEM_MATCH, _dbObj); return this; }
From source file:net.ymate.platform.persistence.mongodb.support.Operator.java
License:Apache License
public Operator elemMatch(Query... queries) { BasicDBObject _dbObj = new BasicDBObject(); for (Query _query : queries) { _dbObj.putAll((BSONObject) _query.toBson()); }//from ww w .ja v a 2s .co m __doAddOperator(IMongo.OPT.ELEM_MATCH, _dbObj); return this; }
From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoStorage.java
License:Open Source License
@Override public <T extends Relation> void declineRelationsOfEntity(Class<T> type, String id, Change change) throws StorageException { if (TypeRegistry.isPrimitiveDomainEntity(type)) { throwTypeIsAPrimitiveException(type); }//from ww w .j a v a 2s . c o m Map<String, Object> propertyMap = Maps.newHashMap(); propertyMap.put(propertyName(type, "accepted"), false); propertyMap.put(DomainEntity.PID, null); BasicDBObject setQuery = new BasicDBObject(); setQuery.putAll(queries.setPropertiesToValue(propertyMap)); setQuery.putAll(queries.incrementRevision()); getDBCollection(type).update(queries.selectRelationsByEntityId(id), setQuery, false, true); }
From source file:org.apache.rya.indexing.mongodb.temporal.TemporalMongoDBStorageStrategy.java
License:Apache License
@Override public DBObject serialize(final RyaStatement ryaStatement) { final BasicDBObject base = (BasicDBObject) super.serialize(ryaStatement); final DBObject time = getTimeValue(ryaStatement.getObject().getData()); base.putAll(time.toMap()); return base;//from w w w .j av a 2s .co m }
From source file:org.canedata.provider.mongodb.entity.MongoEntity.java
License:Apache License
public int update(Serializable... keys) { if (logger.isDebug()) logger.debug("Updating entitiy, Database is {0}, Collection is {1}, keys is {2}.", getSchema(), getName(), Arrays.toString(keys)); try {// ww w.j a v a 2 s .co m if (keys == null || keys.length == 0) return 0; validateState(); final BasicDBObject fields = new BasicDBObject(); final BasicDBObject othersFields = new BasicDBObject(); final BasicDBObject options = new BasicDBObject(); getIntent().playback(new Tracer() { public Tracer trace(Step step) throws AnalyzeBehaviourException { switch (step.step()) { case MongoStep.PUT: if (logger.isDebug()) logger.debug("Analyzing behivor PUT, step is {0}, purpose is {1}, scalar is {2}.", step.step(), step.getPurpose(), Arrays.toString(step.getScalar())); if (StringUtils.isBlank(step.getPurpose())) break; Object val = (step.getScalar() == null || step.getScalar().length == 0) ? null : step.getScalar()[0]; if (step.getPurpose().matches(internalCmds)) othersFields.append(step.getPurpose(), val); else fields.append(step.getPurpose(), val); break; case MongoStep.OPTION: options.append(step.getPurpose(), step.getScalar()[0]); break; default: logger.warn("Step {0} does not apply to activities create, this step will be ignored.", step.step()); } return this; } }); BasicDBObject fs = new BasicDBObject(); if (!fields.isEmpty()) fs.put("$set", fields); if (!othersFields.isEmpty()) fs.putAll(othersFields.toMap()); if (fs.isEmpty()) return 0; WriteResult wr = getCollection().update(new BasicDBObject().append("_id", keys[0]), fs, options.getBoolean(Options.UPSERT, false), false, getCollection().getWriteConcern()); if (!StringUtils.isBlank(wr.getError())) throw new DataAccessException(wr.getError()); // invalidate cache if (null != getCache()) { String cacheKey = getKey().concat("#").concat(keys[0].toString()); getCache().remove(cacheKey); if (logger.isDebug()) logger.debug("Invalidated cache key is {0}.", keys[0].toString()); } return wr.getN(); } catch (AnalyzeBehaviourException abe) { if (logger.isDebug()) logger.debug(abe, "Analyzing behaviour failure, cause by: {0}.", abe.getMessage()); throw new RuntimeException(abe); } finally { getIntent().reset(); } }
From source file:org.canedata.provider.mongodb.entity.MongoEntity.java
License:Apache License
public int updateRange(Expression expr) { if (logger.isDebug()) logger.debug("Updating entities, Database is {0}, Collection is {1} ...", getSchema(), getName()); try {/*from w w w. ja v a 2s.c o m*/ validateState(); final BasicDBObject fields = new BasicDBObject(); final BasicDBObject othersFields = new BasicDBObject(); final BasicDBObject options = new BasicDBObject(); getIntent().playback(new Tracer() { public Tracer trace(Step step) throws AnalyzeBehaviourException { switch (step.step()) { case MongoStep.PUT: if (StringUtils.isBlank(step.getPurpose())) break; Object val = (step.getScalar() == null || step.getScalar().length == 0) ? null : step.getScalar()[0]; if (step.getPurpose().matches(internalCmds)) othersFields.append(step.getPurpose(), val); else fields.append(step.getPurpose(), val); break; case MongoStep.OPTION: options.append(step.getPurpose(), step.getScalar()[0]); break; default: logger.warn("Step {0} does not apply to activities create, this step will be ignored.", step.step()); } return this; } }); final MongoExpressionFactory expFactory = new MongoExpressionFactory.Impl(); BasicDBObject query = expFactory.parse((MongoExpression) expr); if (logger.isDebug()) logger.debug("Updating entities, Database is {0}, Collection is {1}, expression is {2} ...", getSchema(), getName(), query.toString()); BasicDBObject fs = new BasicDBObject(); if (!fields.isEmpty()) fs.append("$set", fields); if (!othersFields.isEmpty()) fs.putAll(othersFields.toMap()); if (fs.isEmpty()) return 0; WriteResult wr = getCollection().update(query, fs, options.getBoolean(Options.UPSERT, false), true, getCollection().getWriteConcern()); if (!StringUtils.isBlank(wr.getError())) throw new DataAccessException(wr.getError()); // invalidate cache if (null != getCache()) { invalidateCache(query); } return wr.getN(); } catch (AnalyzeBehaviourException abe) { if (logger.isDebug()) logger.debug(abe, "Analyzing behaviour failure, cause by: {0}.", abe.getMessage()); throw new RuntimeException(abe); } finally { getIntent().reset(); } }
From source file:org.jongo.Update.java
License:Apache License
private void removeIdField(DBObject updateDbo) { DBObject pojoAsDbo = (DBObject) updateDbo.get("$set"); if (pojoAsDbo.containsField("_id")) { // Need to materialize lazy objects which are read only if (pojoAsDbo instanceof LazyBSONObject) { BasicDBObject expanded = new BasicDBObject(); expanded.putAll(pojoAsDbo); updateDbo.put("$set", expanded); pojoAsDbo = expanded;// w w w .j a va 2 s. c om } pojoAsDbo.removeField("_id"); } }
From source file:org.opencb.opencga.storage.mongodb.alignment.CoverageMongoDBWriter.java
License:Apache License
private void secureInsert(DBObject query, DBObject object, String chromosome, int start, int size) { boolean documentExists = true; boolean fileExists = true; //Check if the document exists {/*from w ww . ja v a 2s .co m*/ QueryResult countId = collection.count(query); if (countId.getNumResults() == 1 && countId.getResultType().equals(Long.class.getCanonicalName())) { if ((Long) countId.getResult().get(0) < 1) { DBObject document = BasicDBObjectBuilder.start().append(FILES_FIELD, new BasicDBList()) .append(CHR_FIELD, chromosome).append(START_FIELD, start).append(SIZE_FIELD, size) .get(); document.putAll(query); //{_id:<chunkId>, files:[]} collection.insert(document, null); //Insert a document with empty files array. fileExists = false; } } else { logger.error(countId.getErrorMsg(), countId); } } if (documentExists) { //Check if the file exists BasicDBObject fileQuery = new BasicDBObject(FILES_FIELD + "." + FILE_ID_FIELD, fileId); fileQuery.putAll(query); QueryResult countFile = collection.count(fileQuery); if (countFile.getNumResults() == 1 && countFile.getResultType().equals(Long.class.getCanonicalName())) { if ((Long) countFile.getResult().get(0) < 1) { fileExists = false; } } else { logger.error(countFile.getErrorMsg(), countFile); } } if (fileExists) { BasicDBObject fileQuery = new BasicDBObject(FILES_FIELD + "." + FILE_ID_FIELD, fileId); fileQuery.putAll(query); BasicDBObject fileObject = new BasicDBObject(); for (String key : object.keySet()) { fileObject.put(FILES_FIELD + ".$." + key, object.get(key)); } // DBObject update = new BasicDBObject("$set", new BasicDBObject(FILES_FIELD, fileObject)); DBObject update = new BasicDBObject("$set", fileObject); //db.<collectionName>.update({_id:<chunkId> , "files.id":<fileId>}, {$set:{"files.$.<objKey>":<objValue>}}) collection.update(fileQuery, update, updateOptions); } else { BasicDBObject fileObject = new BasicDBObject(FILE_ID_FIELD, fileId); fileObject.putAll(object); DBObject update = new BasicDBObject("$addToSet", new BasicDBObject(FILES_FIELD, fileObject)); //db.<collectionName>.update({_id:<chunkId>} , {$addToSet:{files:{id:<fileId>, <object>}}}) collection.update(query, update, updateOptions); } }