com.mongodb.BasicDBObject Java Examples
The following examples show how to use
com.mongodb.BasicDBObject.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MongodbLocalServerIntegrationTest.java From hadoop-mini-clusters with Apache License 2.0 | 7 votes |
@Test public void testMongodbLocalServer() throws Exception { MongoClient mongo = new MongoClient(mongodbLocalServer.getIp(), mongodbLocalServer.getPort()); DB db = mongo.getDB(propertyParser.getProperty(ConfigVars.MONGO_DATABASE_NAME_KEY)); DBCollection col = db.createCollection(propertyParser.getProperty(ConfigVars.MONGO_COLLECTION_NAME_KEY), new BasicDBObject()); col.save(new BasicDBObject("testDoc", new Date())); LOG.info("MONGODB: Number of items in collection: {}", col.count()); assertEquals(1, col.count()); DBCursor cursor = col.find(); while(cursor.hasNext()) { LOG.info("MONGODB: Document output: {}", cursor.next()); } cursor.close(); }
Example #2
Source File: AggregationTest.java From bugu-mongo with Apache License 2.0 | 6 votes |
/** * mark the book as cheap or expensive. */ //@Test public void testCond(){ connectDB(); BookDao dao = new BookDao(); BuguAggregation agg = dao.aggregate(); DBObject cond = ExpressionBuilder.cond().ifCondition("{'$lt':['$price', 10]}").thenValue("cheap").elseValue("expensive").build(); DBObject p = new BasicDBObject(); p.put("title", 1); p.put("price", cond); Iterable<DBObject> it = agg.project(p).results(); for(DBObject dbo : it){ System.out.print(dbo.get("title")); System.out.print(" : "); System.out.println(dbo.get("price")); } disconnectDB(); }
Example #3
Source File: BuguDao.java From bugu-mongo with Apache License 2.0 | 6 votes |
/** * Atomically modify and return a single document. * @param id * @param updater the modifications to apply * @param returnNew when true, returns the modified document rather than the original * @return */ public T findAndModify(String id, BuguUpdater updater, boolean returnNew){ DBObject query = new BasicDBObject(); query.put(Operator.ID, IdUtil.toDbId(clazz, id)); DBObject result = getCollection().findAndModify(query, null, null, false, updater.getModifier(), returnNew, false); T t = MapperUtil.fromDBObject(clazz, result); if(hasCustomListener){ if(returnNew){ notifyUpdated((BuguEntity)t); }else{ BuguEntity entity = (BuguEntity)findOne(id); notifyUpdated(entity); } } return t; }
Example #4
Source File: MongoFieldMapping.java From bluima with Apache License 2.0 | 6 votes |
static void writeFieldToDb(String range, BasicDBObject o, Annotation a, String dbKey, Feature f) { if (range.equals("String")) { o.put(dbKey, a.getStringValue(f)); } else if (range.equals("StringArray")) { StringArray sa = (StringArray) a.getFeatureValue(f); if (sa != null) { String[] vals = sa.toArray(); o.put(dbKey, Lists.newArrayList(vals)); } } else if (range.equals("Integer")) { o.put(dbKey, a.getIntValue(f)); } else if (range.equals("Float")) { o.put(dbKey, a.getFloatValue(f)); } else if (range.equals("Boolean")) { o.put(dbKey, a.getBooleanValue(f)); } else { LOG.warn("range not supported " + range); } }
Example #5
Source File: MongoDB.java From act with GNU General Public License v3.0 | 6 votes |
private List<Seq> keywordInSequence(String in_field, String keyword) { List<Seq> seqs = new ArrayList<Seq>(); BasicDBObject query = new BasicDBObject(); query.put(in_field, keyword); BasicDBObject keys = new BasicDBObject(); DBCursor cur = this.dbSeq.find(query, keys); while (cur.hasNext()) { DBObject o = cur.next(); seqs.add( convertDBObjectToSeq(o) ); } cur.close(); return seqs; }
Example #6
Source File: WriteContext.java From pinpoint with Apache License 2.0 | 6 votes |
private Map<String, ?> getBsonKeyValueMap(Object bson) { if (bson instanceof BasicDBObject) { return (BasicDBObject) bson; } else if (bson instanceof BsonDocument) { return (BsonDocument) bson; } else if (bson instanceof Document) { return (Document) bson; } else { logger.debug("bson KV is null {} ", bson.getClass().getName()); return null; } //TODO leave comments for further use // if(arg instanceof BsonDocumentWrapper) { // bson.append(arg.toString()); // } // if(arg instanceof CommandResult) { // bson.append(arg.toString()); // } // if(arg instanceof RawBsonDocument) { // bson.append(arg.toString()); // } }
Example #7
Source File: MongoDB.java From act with GNU General Public License v3.0 | 6 votes |
public List<Seq> getSeqWithRxnRef(Long rxnId) { List<Seq> seqs = new ArrayList<>(); BasicDBObject query = new BasicDBObject(); query.put("rxn_refs", rxnId); DBCursor cur = this.dbSeq.find(query, new BasicDBObject()); try { while (cur.hasNext()) { DBObject o = cur.next(); seqs.add(convertDBObjectToSeq(o)); } } finally { if (cur != null) { cur.close(); } } return seqs; }
Example #8
Source File: EntityRepositoryTest.java From secure-data-service with Apache License 2.0 | 6 votes |
@Test public void testUpdateRetry() { TenantContext.setTenantId("SLIUnitTest"); repository.deleteAll("student", null); DBObject indexKeys = new BasicDBObject("body.cityOfBirth", 1); mongoTemplate.getCollection("student").ensureIndex(indexKeys); repository.create("student", buildTestStudentEntity()); Entity entity = repository.findOne("student", new NeutralQuery()); Map<String, Object> studentBody = entity.getBody(); studentBody.put("cityOfBirth", "ABC"); Entity studentEntity = new MongoEntity("student", entity.getEntityId(), studentBody, entity.getMetaData()); repository.updateWithRetries("student", studentEntity, 5); NeutralQuery neutralQuery = new NeutralQuery(); neutralQuery.addCriteria(new NeutralCriteria("cityOfBirth=ABC")); assertEquals(1, repository.count("student", neutralQuery)); repository.deleteAll("student", null); mongoTemplate.getCollection("student").dropIndex(indexKeys); }
Example #9
Source File: QueryBuilder.java From XBDD with Apache License 2.0 | 6 votes |
public BasicDBObject getSearchQuery(final List<String> searchWords, final Coordinates coordinates, final String[] searchCategories) { final List<BasicDBObject> searchParameters = new ArrayList<>(); for (int i = 0; i < searchWords.size(); i++) { String key = searchWords.get(i); if (!key.equals("")) { Pattern matchPattern; try { matchPattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE); } catch (final PatternSyntaxException e) { key = Pattern.quote(key); searchWords.set(i, key); matchPattern = Pattern.compile(key); } for (final String searchCategory : searchCategories) { searchParameters.add(new BasicDBObject(searchCategory, matchPattern)); } } } return coordinates.getQueryObject().append("$or", searchParameters); }
Example #10
Source File: MongoDBOutputOperator.java From attic-apex-malhar with Apache License 2.0 | 6 votes |
/** * At setup time, init last completed windowId from maxWindowTable * * @param context */ @Override public void setup(OperatorContext context) { operatorId = context.getId(); try { mongoClient = new MongoClient(hostName); db = mongoClient.getDB(dataBase); if (userName != null && passWord != null) { db.authenticate(userName, passWord.toCharArray()); } initLastWindowInfo(); for (String table : tableList) { tableToDocumentList.put(table, new ArrayList<DBObject>()); tableToDocument.put(table, new BasicDBObject()); } } catch (UnknownHostException ex) { logger.debug(ex.toString()); } }
Example #11
Source File: MongoDbDeltaStore.java From swellrt with Apache License 2.0 | 6 votes |
@Override public void delete(WaveletName waveletName) throws PersistenceException, FileNotFoundPersistenceException { BasicDBObject criteria = new BasicDBObject(); criteria.put(MongoDbDeltaStoreUtil.FIELD_WAVE_ID, waveletName.waveId.serialise()); criteria.put(MongoDbDeltaStoreUtil.FIELD_WAVELET_ID, waveletName.waveletId.serialise()); try { // Using Journaled Write Concern // (http://docs.mongodb.org/manual/core/write-concern/#journaled) deltasCollection.withWriteConcern(WriteConcern.JOURNALED).deleteMany(criteria); } catch (MongoException e) { throw new PersistenceException(e); } // Also delete wavelet snapshots snapshotStore.deleteSnapshot(waveletName); }
Example #12
Source File: Group.java From sample-acmegifts with Eclipse Public License 1.0 | 6 votes |
/** * Create a Mongo DB Object baed on the content of this group * * @param id The Mongo Object id to assign to this DB Object. If null, a new Object id will be * created * @return - The Mongo DB Object based on the content of this group */ public BasicDBObject getDBObject(boolean includeId) { BasicDBObject group = new BasicDBObject(); if (includeId) { group.append(DB_ID, new ObjectId(id)); } group.append(JSON_KEY_GROUP_NAME, name); BasicDBList membersArray = new BasicDBList(); for (int i = 0; i < members.length; i++) { membersArray.add(members[i]); } group.append(JSON_KEY_MEMBERS_LIST, membersArray); return group; }
Example #13
Source File: DAO.java From Babler with Apache License 2.0 | 6 votes |
/** * checks if an entry exists in the dbName DB * @param entry to check * @param dbName to check in * @return true if new */ public static boolean isNew(DBEntry entry, String dbName){ if(entry == null) return false; MongoDatabase db = MongoDB.INSTANCE.getDatabase(dbName); String collectionName = getCollectionName(entry); MongoCollection collection = db.getCollection(collectionName,BasicDBObject.class); BasicDBObject obj = (BasicDBObject) collection.find(eq("_id", entry.getId())).first(); if(obj != null) return false; else return true; }
Example #14
Source File: QueryModel.java From birt with Eclipse Public License 1.0 | 6 votes |
private static void validateAggregateCommand( DBObject commandObj ) throws OdaException { // validate a $group pipeline operation expression, if specified List<BasicDBObject> groupOps = findPipelineOperation( commandObj, GROUP_AGGR_KEY ); for( BasicDBObject groupOp : groupOps ) { if( ! groupOp.containsField( DOC_ID_FIELD_NAME ) ) throw new OdaException( Messages.bind( Messages.queryModel_missingGroupAggrKey, new Object[]{ GROUP_AGGR_KEY, DOC_ID_FIELD_NAME, groupOp } ) ); } // validate a $sort pipeline operation expression, if specified List<BasicDBObject> sortOps = findPipelineOperation( commandObj, SORT_AGGR_KEY ); for( BasicDBObject sortOp : sortOps ) { for( Object sortKeySpec : sortOp.values() ) { if( sortKeySpec instanceof Number ) { int sortKeyValue = ((Number)sortKeySpec).intValue(); if( sortKeyValue == 1 || sortKeyValue == -1 ) continue; // is valid } throw new OdaException( Messages.bind( Messages.queryModel_invalidSortAggrValue, SORT_AGGR_KEY, sortOp ) ); } } }
Example #15
Source File: MongoWrapperDefaultHandler.java From DBus with Apache License 2.0 | 6 votes |
/** * 根据oid去数据库回查数据 * * @param oid * @return */ private Document fetchData(String schemaName, String tableName, String oid) { Document result = null; DbusDatasource datasource = GlobalCache.getDatasource(); MongoClientURI uri = new MongoClientURI(datasource.getMasterUrl()); MongoClient client = new MongoClient(uri); MongoDatabase database = client.getDatabase(schemaName); MongoCollection<Document> collection = database.getCollection(tableName); MongoCursor<Document> cursor = collection.find(new BasicDBObject().append("_id", new ObjectId(oid))).iterator(); if (cursor.hasNext()) { result = cursor.next(); } else { logger.error("get source data error. schemaName:{}, tableName:{}, oid:{}", schemaName, tableName, oid); } client.close(); return result; }
Example #16
Source File: ReachablesProjectionUpdate.java From act with GNU General Public License v3.0 | 6 votes |
public void updateDatabase(DBCollection reachables) { for (String product : products) { // The query object for this product BasicDBObject newProductQuery = new BasicDBObject().append(INCHI_KEY, product); // DB list of the substrates of this projection BasicDBList substrateList = new BasicDBList(); substrateList.addAll(substrates); // DB list of the one RO associated with this projection BasicDBList roList = new BasicDBList(); roList.addAll(ros); // The full entry to be added to the product's precursor list BasicDBObject precursorEntry = new BasicDBObject() .append(SUBSTRATES_KEY, substrateList) .append(RO_KEY, roList); // The command to push the precursor entry onto the precursor list BasicDBObject precursors = new BasicDBObject(); precursors.append("$push", new BasicDBObject(PRECURSOR_KEY, precursorEntry)); // Do the update! reachables.update(newProductQuery, precursors, UPSERT, NO_MULTI); } }
Example #17
Source File: MarketDataServiceBasicImpl.java From redtorch with MIT License | 6 votes |
@Override public List<BarField> queryTodayBar5MinList(long startTimestamp, long endTimestamp, String unifiedSymbol) { try { Document filter = new Document(); Document dateDocument = new Document(); dateDocument.put("$gte", startTimestamp); dateDocument.put("$lte", endTimestamp); filter.put("actionTimestamp", dateDocument); filter.put("unifiedSymbol", unifiedSymbol); BasicDBObject sortBO = new BasicDBObject(); sortBO.put("actionTimestamp", 1); long beginTime = System.currentTimeMillis(); List<Document> documentList = this.todayMarketDataDBClient.find(todayMarketDataDBName, COLLECTION_NAME_BAR_5_MIN, filter, sortBO); logger.info("查询Bar数据,数据库{},集合{},操作耗时{}ms,共{}条数据", todayMarketDataDBName, COLLECTION_NAME_BAR_5_MIN, (System.currentTimeMillis() - beginTime), documentList.size()); return documentListToBarList(documentList, MarketDataDBTypeEnum.MDDT_TD.getValueDescriptor().getName()); } catch (Exception e) { logger.error("查询当日5分钟数据发生错误", e); } return new ArrayList<>(); }
Example #18
Source File: MarcMongodbClientTest.java From metadata-qa-marc with GNU General Public License v3.0 | 5 votes |
public void testInsert() throws UnknownHostException { MarcMongodbClient client = new MarcMongodbClient("localhost" , 27017, "sub_last_print"); DBCollection collection = client.getCollection("marc"); BasicDBObject doc = createTestObject(); collection.insert(doc); assertNotNull(collection); assertEquals(1, collection.count()); collection.remove(new BasicDBObject("name", "MongoDB")); assertEquals(0, collection.count()); }
Example #19
Source File: MongoDB.java From act with GNU General Public License v3.0 | 5 votes |
/** * establishes new numbering system for organisms not already in our database * @param name the name of the organism to be added to the database * @return the id of the new organism added to the database */ public Long submitToActOrganismNameDB(String name) { BasicDBObject doc = new BasicDBObject(); Long id = this.dbOrganismNames.count() + ORG_ID_BASE; doc.put("org_id", id); doc.put("name", name); // TODO: support NCBI ids too. if (this.dbOrganismNames == null) { System.out.print("Organism: " + name); return null; } else { this.dbOrganismNames.insert(doc); return id; } }
Example #20
Source File: Group.java From sample-acmegifts with Eclipse Public License 1.0 | 5 votes |
public boolean isEqual(BasicDBObject other) { BasicDBList oMembers = (BasicDBList) other.get(JSON_KEY_MEMBERS_LIST); return ((oMembers.containsAll(Arrays.asList(this.members)) && oMembers.size() == this.members.length) && this.name.equals(other.get(JSON_KEY_GROUP_NAME)) && this.id.equals(other.getString(DB_ID))); }
Example #21
Source File: EncryptSystemTest.java From spring-data-mongodb-encrypt with Apache License 2.0 | 5 votes |
@Test public void checkNonEncryptedMap() { MyBean bean = new MyBean(); Map<String, MySubBean> map = new HashMap<>(); map.put("one", new MySubBean("sky is blue", " earth is round")); map.put("two", new MySubBean("grass is green", "earth is flat")); bean.nonSensitiveMap = map; mongoTemplate.save(bean); MyBean fromDb = mongoTemplate.findOne(query(where("_id").is(bean.id)), MyBean.class); assertThat(fromDb.nonSensitiveMap.get("one").secretString, is(bean.nonSensitiveMap.get("one").secretString)); assertThat(fromDb.nonSensitiveMap.get("one").nonSensitiveData, is(bean.nonSensitiveMap.get("one").nonSensitiveData)); assertThat(fromDb.nonSensitiveMap.get("two").secretString, is(bean.nonSensitiveMap.get("two").secretString)); assertThat(fromDb.nonSensitiveMap.get("two").nonSensitiveData, is(bean.nonSensitiveMap.get("two").nonSensitiveData)); Document fromMongo = mongoTemplate.getCollection(MyBean.MONGO_MYBEAN).find(new BasicDBObject("_id", new ObjectId(bean.id))).first(); Document mapMongo = (Document) fromMongo.get(MyBean.MONGO_NONSENSITIVEMAP); Document oneMongo = (Document) mapMongo.get("one"); Document twoMongo = (Document) mapMongo.get("two"); assertThat(oneMongo.get(MySubBean.MONGO_NONSENSITIVEDATA), is(map.get("one").nonSensitiveData)); assertThat(twoMongo.get(MySubBean.MONGO_NONSENSITIVEDATA), is(map.get("two").nonSensitiveData)); assertCryptLength(oneMongo.get(MySubBean.MONGO_SECRETSTRING), map.get("one").secretString.length() + 12); assertCryptLength(twoMongo.get(MySubBean.MONGO_SECRETSTRING), map.get("two").secretString.length() + 12); }
Example #22
Source File: Operator.java From ymate-platform-v2 with Apache License 2.0 | 5 votes |
public Operator elemMatch(Operator... operators) { BasicDBObject _dbObj = new BasicDBObject(); for (Operator _opt : operators) { _dbObj.putAll((BSONObject) _opt.toBson()); } __doAddOperator(IMongo.OPT.ELEM_MATCH, _dbObj); return this; }
Example #23
Source File: InsertVo.java From tangyuan2 with GNU General Public License v3.0 | 5 votes |
public Object insert(DBCollection collection, WriteConcern writeConcern) { DBObject document = new BasicDBObject(); // 匹配_id for (int i = 0, n = columns.size(); i < n; i++) { // document.put(columns.get(i), values.get(i).getValue()); String tempColumn = columns.get(i); if (3 == tempColumn.length() && tempColumn.equals("_id")) { document.put(tempColumn, new ObjectId(values.get(i).getValue().toString())); } else { document.put(tempColumn, values.get(i).getValue()); } } log(document); // TODO: WriteConcern.ACKNOWLEDGED需要可以配置 // WriteResult result = collection.insert(document, WriteConcern.ACKNOWLEDGED); // collection.insert(document, MongoComponent.getInstance().getDefaultWriteConcern()); collection.insert(document, writeConcern); Object oid = document.get("_id"); if (null != oid) { return oid.toString(); } return null; }
Example #24
Source File: BatchJobMongoDA.java From secure-data-service with Apache License 2.0 | 5 votes |
@Override public void updateRecordHash(RecordHash rh, String newHashValues) throws DataAccessResourceFailureException { rh.setHash(newHashValues); rh.setUpdated(System.currentTimeMillis()); rh.setVersion(rh.getVersion() + 1); // Detect tenant collision - should never occur since tenantId is in the hash TenantContext.setIsSystemCall(false); this.sliMongo.getCollection(RECORD_HASH).update(recordHashQuery(rh.getId()).getQueryObject(), new BasicDBObject(rh.toKVMap())); }
Example #25
Source File: MongoDB.java From act with GNU General Public License v3.0 | 5 votes |
public NamesOfMolecule fetchNamesFromInchi(String inchi) { BasicDBObject whereQuery = new BasicDBObject("InChI", inchi); BasicDBObject fields = new BasicDBObject(); fields.put("InChI", true); fields.put("names.brenda", true); fields.put("xref.CHEBI.metadata.Synonym", true); fields.put("xref.DRUGBANK.metadata", true); fields.put("xref.METACYC.meta", true); fields.put("xref.WIKIPEDIA.metadata.article", true); BasicDBObject c = (BasicDBObject) dbChemicals.findOne(whereQuery, fields); if (c == null) { return null;} NamesOfMolecule moleculeNames = getNamesFromBasicDBObject(c); return moleculeNames; }
Example #26
Source File: MongoDBOutputOperator.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
/** * Implement Operator Interface. * If windowId is less than the last completed windowId, then ignore the window. * If windowId is equal to the last completed windowId, then remove the documents with same windowId of the operatorId, and insert the documents later * If windowId is greater then the last completed windowId, then process the window * * @param windowId */ @Override public void beginWindow(long windowId) { this.windowId = windowId; tupleId = 1; if (windowId < lastWindowId) { ignoreWindow = true; } else if (windowId == lastWindowId) { ignoreWindow = false; BasicDBObject query = new BasicDBObject(); // query.put(windowIdColumnName, windowId); // query.put(operatorIdColumnName, operatorId); ByteBuffer bb = ByteBuffer.allocate(12); bb.order(ByteOrder.BIG_ENDIAN); StringBuilder low = new StringBuilder(); StringBuilder high = new StringBuilder(); if (queryFunction == 1) { queryFunction1(bb, high, low); } else if (queryFunction == 2) { queryFunction2(bb, high, low); } else if (queryFunction == 3) { queryFunction3(bb, high, low); } else { throw new RuntimeException("unknown queryFunction type:" + queryFunction); } query.put("_id", new BasicDBObject("$gte", new ObjectId(low.toString())).append("$lte", new ObjectId(high.toString()))); // query.put(applicationIdName, 0); for (String table : tableList) { db.getCollection(table).remove(query); } } else { ignoreWindow = false; } }
Example #27
Source File: BuguDao.java From bugu-mongo with Apache License 2.0 | 5 votes |
private DBObject getReturnFields(String... fields){ DBObject dbo = new BasicDBObject(); for(String f : fields){ dbo.put(f, 1); } return dbo; }
Example #28
Source File: BuguDao.java From bugu-mongo with Apache License 2.0 | 5 votes |
private DBObject getNotReturnFields(String... fields){ DBObject dbo = new BasicDBObject(); for(String f : fields){ dbo.put(f, 0); } return dbo; }
Example #29
Source File: DefaultAsyncService.java From hvdf with Apache License 2.0 | 5 votes |
public DefaultAsyncService(final MongoClientURI dbUri, final AsyncServiceConfiguration config){ super(dbUri, config); this.config = config; this.recoveryColl = this.database.getCollection(config.recovery_collection_name); this.recoveryColl.createIndex( new BasicDBObject(RecoveryRecord.STATE_KEY, 1)); // Get the identifier for this processor instance if(config.service_signature.isEmpty() == true){ signature = ManagementFactory.getRuntimeMXBean().getName(); } else { signature = config.service_signature; } // If the processing thread pool is configured, create an executor final int threadCount = config.processing_thread_pool_size; if(threadCount > 0){ createExecutor(threadCount); } else { this.executor = null; } // If recovery is enabled, set the recovery timer if(this.executor != null && config.recovery_poll_time >= 0){ this.recoveryTimer = new Timer(); this.recoveryTimer.schedule( new RecoveryTimerTask(this), config.recovery_poll_time); } else { this.recoveryTimer = null; } }
Example #30
Source File: MongodbImplStrategy.java From uavstack with Apache License 2.0 | 5 votes |
@Override public void concretProcessor(Object expKey, Map expValue, BasicDBObject set) { Map KVMap = (Map) expValue.get(expKey); Set keyset = KVMap.keySet(); Iterator iter = keyset.iterator(); String key = null; BasicDBObject content = new BasicDBObject(); while (iter.hasNext()) { key = (String) iter.next(); content.append(key, KVMap.get(key)); } switch (expKey.toString()) { case "set": set.append("$set", content); break; case "unset": set.append("$unset", content); break; case "rename": set.append("$rename", content); break; case "push": set.append("$push", content); break; case "pull": set.append("$pull", content); break; default: set.append("$" + expKey.toString(), content); break; } log.info(this, "set :" + set.toJson()); }