while (cursor.hasNext()) { final DBObject value = cursor.next(); metricsData.put("node", value.get("node")); final MetricType metricType = MetricType.valueOf(((String) value.get("type")).toUpperCase(Locale.ENGLISH)); Map<String, Object> dataPoint = Maps.newHashMap(); values.add(dataPoint); dataPoint.put("timestamp", value.get("timestamp")); metricsData.put("type", metricType.toString().toLowerCase(Locale.ENGLISH)); final Object gaugeValue = value.get("value"); dataPoint.put("value", gaugeValue); break; case COUNTER: dataPoint.put("count", value.get("count")); break; case HISTOGRAM: dataPoint.put("75-percentile", value.get("75-percentile")); dataPoint.put("95-percentile", value.get("95-percentile")); dataPoint.put("98-percentile", value.get("98-percentile")); dataPoint.put("99-percentile", value.get("99-percentile")); dataPoint.put("999-percentile", value.get("999-percentile")); dataPoint.put("max", value.get("max")); dataPoint.put("min", value.get("min")); dataPoint.put("mean", value.get("mean")); dataPoint.put("median", value.get("median")); dataPoint.put("std_dev", value.get("std_dev")); break;
@Override @Nullable public MongoDbSession load(String sessionId) { DBObject query = new BasicDBObject(); query.put(MongoDbSession.FIELD_SESSION_ID, sessionId); DBObject result = findOne(MongoDbSession.class, query); if (result == null) { return null; } final Object objectId = result.get("_id"); return new MongoDbSession((ObjectId) objectId, result.toMap()); }
BasicDBList modifiedList = new BasicDBList(); for (Object value : dbList) { if (value instanceof DBObject) { modifiedList.add(encodeReservedCharacteres((DBObject) value)); } else { modifiedList.add(value); Set<String> keySet = profileBody.keySet(); DBObject modifiedNode = new BasicDBObject(); if (keySet != null) { for (String key : keySet) { Object value = profileBody.get(key); for (char symbolToReplace : RESERVED_CHARACTERS.keySet()) { key = key.replace(symbolToReplace, RESERVED_CHARACTERS.get(symbolToReplace)); modifiedNode.put(key, encodeReservedCharacteres((DBObject) value)); } else { modifiedNode.put(key, value);
private boolean filterMatch(DBObject filter, DBObject object) { for (String key : filter.keySet()) { if (!object.containsField(key)) { return false; } if (!filter.get(key).equals(object.get(key))) { return false; } } return true; }
public BasicDBList getList(String key) { DBCollection coll = getCollection(); DBObject query = new BasicDBObject(); query.put("key", key); DBObject result = coll.findOne(query); return (BasicDBList) result.get("value"); }
MongoClient mongo = new MongoClient( "localhost" , 27017 ); DB db = mongo.getDB(dbName); DBCollection collection = db.getCollection(collectionName); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("movie_id", id); DBObject document = collection.findOne(whereQuery); BasicDBList list = (BasicDBList) document.get("genre"); List<String> res = new ArrayList<String>(); for(Object el: list) { res.add((String) el); }
cr = mongoAdminDB.command(new BasicDBObject("addShard", command)); logger.info(cr.toString()); cr = mongoAdminDB.command(new BasicDBObject("listShards", 1)); logger.info(cr.toString()); cr = mongoAdminDB.command(new BasicDBObject("enableSharding", this.shardDatabase)); logger.info(cr.toString()); DB db = mongo.getDB(this.shardDatabase); db.getCollection(this.shardCollection).createIndex(this.shardKey); DBObject cmd = new BasicDBObject(); cmd.put("shardCollection", this.shardDatabase + "." + this.shardCollection); cmd.put("key", new BasicDBObject(this.shardKey, 1)); cr = mongoAdminDB.command(cmd); logger.info(cr.toString()); DBCursor cursor = mongo.getDB("config").getCollection("shards").find(); while (cursor.hasNext()) { DBObject item = cursor.next(); logger.info(item.toString());
MongoClient mongoClient = new MongoClient("SERVER", 27017); DB db = mongoClient.getDB("DB_NAME"); DBCollection coll1 = db.getCollection("COLLECTION_NAME"); DBObject eleMatch = new BasicDBObject(); eleMatch.put("url", "www.pqr.com"); eleMatch.put("type", new BasicDBObject("$lte", 50)); BasicDBObject up = new BasicDBObject(); up.put("$elemMatch", eleMatch); BasicDBList basicDBList = new BasicDBList(); basicDBList.add(up); DBObject query = new BasicDBObject("Data", new BasicDBObject(" $all", basicDBList)); coll1.find(query);
.command(new BasicDBObject("isMaster", 1)); logger.info("isMaster: {}", cr); DBObject replicaSetSetting = new BasicDBObject(); replicaSetSetting.put("_id", replicaName); BasicDBList members = new BasicDBList(); int i = 0; for (IMongodConfig mongoConfig : mongoConfigList) { DBObject host = new BasicDBObject(); host.put("_id", i++); host.put("host", mongoConfig.net().getServerAddress().getHostName() + ":" + mongoConfig.net().getPort()); members.add(host); replicaSetSetting.put("members", members); logger.info(replicaSetSetting.toString()); cr = mongoAdminDB.command(new BasicDBObject("replSetInitiate", replicaSetSetting)); logger.info("replSetInitiate: {}", cr); cr = mongoAdminDB.command(new BasicDBObject("replSetGetStatus", 1)); logger.info("replSetGetStatus: {}", cr); mongo.close(); mongo = null;
MongoClient mongo = new MongoClient("localhost", 27017); DB db = (DB) mongo.getDB("testDB"); DBCollection collection = db.getCollection("collection"); DBObject query = new BasicDBObject("_id", "10-100-5675234"); DBObject update = new BasicDBObject(); update.put("$unset", new BasicDBObject("userId","")); WriteResult result = collection.update(query, update); mongo.close();
logger.info("MongoDBRiver is beginning initial import of " + collection.getFullName()); boolean inProgress = true; String lastId = null; if (logger.isTraceEnabled()) { logger.trace("Collection {} - count: {}", collection.getName(), safeCount(collection, timestamp.getClass())); .find(getFilterForInitialImport(definition.getMongoCollectionFilter(), lastId)) .sort(new BasicDBObject("_id", 1)); while (cursor.hasNext() && context.getStatus() == Status.RUNNING) { DBObject object = cursor.next(); count++; if (cursor.hasNext()) { GridFS grid = new GridFS(mongoClient.getDB(definition.getMongoDb()), definition.getMongoCollection()); DBObject object = cursor.next(); if (object instanceof GridFSDBFile) { GridFSDBFile file = grid.findOne(new ObjectId(object.get(MongoDBRiver.MONGODB_ID_FIELD).toString())); if (cursor.hasNext()) { lastId = addInsertToStream(null, file);
public static Object getNextSequence(String name) throws Exception{ MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); // Now connect to your databases DB db = mongoClient.getDB("demo"); DBCollection collection = db.getCollection("counters"); BasicDBObject find = new BasicDBObject(); find.put("_id", name); BasicDBObject update = new BasicDBObject(); update.put("$inc", new BasicDBObject("seq", 1)); DBObject obj = collection.findAndModify(find, update); return obj.get("seq");
@Override public Collection<MongoDbSession> loadAll() { DBObject query = new BasicDBObject(); List<MongoDbSession> dbSessions = Lists.newArrayList(); final List<DBObject> sessions = query(MongoDbSession.class, query); for (DBObject session : sessions) { dbSessions.add(new MongoDbSession((ObjectId) session.get("_id"), session.toMap())); } return dbSessions; } }
@Test public void testRecursiveReference() { final DBCollection stuff = getDb().getCollection("stuff"); stuff.save(parentDbObj); stuff.save(childDbObj); stuff.findOne(new BasicDBObject("_id", parentDbObj.get("_id"))), new DefaultEntityCache()); final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject("_id", childDbObj.get("_id"))), new DefaultEntityCache()); stuff.findOne(new BasicDBObject("_id", parentDbObj.get("_id"))), new DefaultEntityCache()); final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject("_id", childDbObj.get("_id"))), new DefaultEntityCache());
final MongoClient mongoClient = new MongoClient(); final DB db = mongoClient.getDB("DB_NAME"); final DBCollection collection = db.getCollection("item"); final DBObject groupIdFields = new BasicDBObject("_id", "$name"); groupIdFields.put("count", new BasicDBObject("$sum", 1)); final DBObject group = new BasicDBObject("$group", groupIdFields); final DBObject projectFields = new BasicDBObject("_id", 0); projectFields.put("name", "$_id"); projectFields.put("count", 1); final DBObject project = new BasicDBObject("$project", projectFields); final AggregationOutput aggregate = collection.aggregate(group, project);
@Override public Map<String, Long> totalCountByType() { final Map<String, Long> inputCountByType = new HashMap<>(); try (DBCursor inputTypes = dbCollection.find(null, new BasicDBObject(MessageInput.FIELD_TYPE, 1))) { for (DBObject inputType : inputTypes) { final String type = (String) inputType.get(MessageInput.FIELD_TYPE); if (type != null) { final Long oldValue = inputCountByType.get(type); final Long newValue = (oldValue == null) ? 1 : oldValue + 1; inputCountByType.put(type, newValue); } } } return inputCountByType; }
@Override public List<String> typeNames() { DBCursor cursor = collection.find( new BasicDBObject(), new BasicDBObject(FeatureTypeDBObject.KEY_typeName, 1)); List<String> typeNames = new ArrayList<String>(cursor.count()); while (cursor.hasNext()) { DBObject document = cursor.next(); if (document != null) { Object typeName = document.get(FeatureTypeDBObject.KEY_typeName); if (typeName instanceof String) { typeNames.add((String) typeName); } } } return typeNames; }
final DBObject persisted = collection.findOne(key.getId()); assertNotNull(persisted); assertEquals("foo", persisted.get("singleRef")); assertEquals("foo", persisted.get("lazySingleRef")); final BasicDBList expectedList = new BasicDBList(); expectedList.add("foo"); expectedList.add("bar"); expectedList.add("baz"); assertEquals(expectedList, persisted.get("collectionRef")); assertEquals(expectedList, persisted.get("lazyCollectionRef")); final DBObject expectedMap = new BasicDBObject(); expectedMap.put("0", "foo"); expectedMap.put("1", "bar"); expectedMap.put("2", "baz"); assertEquals(expectedMap, persisted.get("mapRef")); assertEquals(expectedMap, persisted.get("lazyMapRef"));
DBCollection coll = db.getCollection("test"); BasicDBObject query = new BasicDBObject("room", "Kitchen"); // optional, limit the fields to only have the lights field BasicDBObject fields = new BasicDBObject("lights",1).append("_id",false); DBCursor curs = coll.find(query, fields); while(curs.hasNext()) { DBObject o = curs.next(); // shows the whole result document System.out.println(o.toString()); BasicDBList lights = (BasicDBList) o.get("lights"); // shows the lights array -- this is actually a collection of DBObjects System.out.println(lights.toString()); // optional: break it into a native java array BasicDBObject[] lightArr = lights.toArray(new BasicDBObject[0]); for(BasicDBObject dbObj : lightArr) { // shows each item from the lights array System.out.println(dbObj); } }
@Override public long totalExtractorCount() { final DBObject query = new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, new BasicDBObject("$exists", true)); long extractorsCount = 0; try (DBCursor inputs = dbCollection.find(query, new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, 1))) { for (DBObject input : inputs) { final BasicDBList extractors = (BasicDBList) input.get(InputImpl.EMBEDDED_EXTRACTORS); extractorsCount += extractors.size(); } } return extractorsCount; }