Refine search
checkPermission(RestPermissions.METRICS_READHISTORY, metricName); BasicDBObject andQuery = new BasicDBObject(); obj.add(new BasicDBObject("name", metricName)); if (after != -1) { obj.add(new BasicDBObject("$gt", new BasicDBObject("$gt", new Date(after)))); andQuery.put("$and", obj); final DBCollection dbCollection = mongoConnection.getDatabase().getCollection("graylog2_metrics"); try(DBCursor cursor = dbCollection.find(andQuery).sort(new BasicDBObject("timestamp", 1))) { final Map<String, Object> metricsData = Maps.newHashMap(); metricsData.put("name", metricName); metricsData.put("values", values); 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));
/** * Finds a list of files matching the given query. * * @param query the filter to apply * @param sort the fields to sort with * @return list of gridfs files * @throws com.mongodb.MongoException if the operation fails */ public List<GridFSDBFile> find(final DBObject query, final DBObject sort) { List<GridFSDBFile> files = new ArrayList<GridFSDBFile>(); DBCursor cursor = filesCollection.find(query); if (sort != null) { cursor.sort(sort); } try { while (cursor.hasNext()) { files.add(injectGridFSInstance(cursor.next())); } } finally { cursor.close(); } return Collections.unmodifiableList(files); }
private Timestamp<?> getCurrentOplogTimestamp(MongoClient shardClient) { DBCollection oplogCollection = shardClient .getDB(MongoDBRiver.MONGODB_LOCAL_DATABASE) .getCollection(MongoDBRiver.OPLOG_COLLECTION); try (DBCursor cursor = oplogCollection.find().sort(new BasicDBObject("$natural", -1)).limit(1)) { return Timestamp.on(cursor.next()); } }
private void fillArray(final int n) { checkIteratorOrArray(IteratorOrArray.ARRAY); while (n >= all.size() && hasNext()) { all.add(nextInternal()); } }
MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("test"); DBCollection coll = db.getCollection("testCollection"); BasicDBObject query = new BasicDBObject("_id", "51a29f6413dc992c24e0283e"); DBCursor cursor = coll.find(query); try { while(cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); }
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());
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()) { lastId = addInsertToStream(null, applyFieldFilter(object), collection.getName()); } else { GridFS grid = new GridFS(mongoClient.getDB(definition.getMongoDb()), definition.getMongoCollection()); while (cursor.hasNext()) { 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); } else { if (cursor != null) { logger.trace("Closing initial import cursor"); cursor.close();
MongoClient client = new MongoClient("10.0.2.113" , 27017); MongoDatabase db = client.getDatabase("maindb"); DBCollection collection = db.getCollection("mainCollection") BasicDBObject document = new BasicDBObject(); // Delete All documents from collection Using blank BasicDBObject collection.remove(document); // Delete All documents from collection using DBCursor DBCursor cursor = collection.find(); while (cursor.hasNext()) { collection.remove(cursor.next()); }
@Override public int countStepStartTimes(final String stepName, final long jobInstanceId) { final DBObject keys = new BasicDBObject(TableColumns.JOBEXECUTIONID, 1); keys.put(TableColumns._id, 0); final DBCursor cursor = db.getCollection(TableColumns.JOB_EXECUTION).find( new BasicDBObject(TableColumns.JOBINSTANCEID, jobInstanceId), keys); final BasicDBList basicDBList = new BasicDBList(); while (cursor.hasNext()) { final DBObject next = cursor.next(); basicDBList.add(next.get(TableColumns.JOBEXECUTIONID)); } final DBObject inClause = new BasicDBObject("$in", basicDBList); final DBObject query = new BasicDBObject(TableColumns.JOBEXECUTIONID, inClause); query.put(TableColumns.STEPNAME, stepName); return db.getCollection(TableColumns.STEP_EXECUTION).find(query).count(); }
DBCollection coll = db.getCollection("minnalPolicyMetadata"); BasicDBObject searchDocument = new BasicDBObject(); searchDocument.put("policyMap.CostCalculation",new BasicDBObject("$exists",true)); DBCursor cursor = coll.find(searchDocument); if(cursor.count() != 0){ while(cursor.hasNext()) System.out.println(cursor.next()); }
Mongo mongo = new Mongo("localhost:27017"); DB db = mongo.getDB("myDB"); DBCollection coll = db.getCollection("myCollection"); DBObject statusQuery = new BasicDBObject("status", true); DBObject elemMatchQuery = new BasicDBObject("$elemMatch", statusQuery); DBObject fields = new BasicDBObject(); fields.put("items", elemMatchQuery); fields.put("name", 1); fields.put("config", 1); DBCursor cur = coll.find(new BasicDBObject(), fields); while (cur.hasNext()) { DBObject obj = cur.next(); // Get fields from object }
public int count(DBObject query) throws CompatibilityDataException { MongoClient client = null; try { client = getService().createClient(); return client.getDB(database).getCollection(collection).find(query).sort(new BasicDBObject("registrationDate", 1)).size(); } catch (Exception ex) { throw new CompatibilityDataException(String.format("(Unknown Host) Failed to fetch object with query %s", query ), ex); } finally { if(client != null) client.close(); } }
@Override public RevObject getIfPresent(ObjectId id) { DBObject query = new BasicDBObject(); query.put("oid", id.toString()); DBCursor results = collection.find(query); if (results.hasNext()) { DBObject result = results.next(); return fromBytes(id, (byte[]) result.get("serialized_object")); } else { return null; } }
private void addRealmCodeSecret() { DBCollection realms = db.getCollection("realms"); DBObject query = new QueryBuilder() .and("codeSecret").is(null).get(); DBCursor objects = realms.find(query); while (objects.hasNext()) { DBObject object = objects.next(); object.put("codeSecret", KeycloakModelUtils.generateCodeSecret()); realms.save(object); log.debugv("Added realm.codeSecret, id={0}", object.get("id")); } }
private List<String> getExecuted(DB db, boolean changeLogExists, DBCollection changeLog) { boolean realmExists = db.collectionExists("realms"); List<String> executed = new LinkedList<>(); if (!changeLogExists && realmExists) { Update1_0_0_Final u = new Update1_0_0_Final(); executed.add(u.getId()); createLog(changeLog, u, 1); } else if (changeLogExists) { DBCursor cursor = changeLog.find().sort(new BasicDBObject("orderExecuted", 1)); while (cursor.hasNext()) { executed.add((String) cursor.next().get("_id")); } } return executed; }
DB db = mongoClient.getDB( "mydb" ); DBCollection collection = db.getCollection("testCollection"); DBObject query = new BasicDBObject("Path", new BasicDBObject("$exists", true)); DBCursor cursor = collection.find(query); try { while(cursor.hasNext()) { System.out.println(cursor.next()); } } finally { cursor.close(); }
DBCursor toCursor(Query q, List<Filter> postFilter, List<String> postFilterAttrs) { DBObject query = new BasicDBObject(); BasicDBObject keys = new BasicDBObject(); for (String p : q.getPropertyNames()) { keys.put(mapper.getPropertyPath(p), 1); LOG.fine(String.format("find(%s, %s)", query, keys)); c = collection.find(query, keys); } else { if (LOG.isLoggable(Level.FINE)) { LOG.fine(String.format("find(%s)", query)); c = collection.find(query); c = c.skip(q.getStartIndex()); c = c.limit(q.getMaxFeatures()); orderBy.append(property, sortBy.getSortOrder() == SortOrder.ASCENDING ? 1 : -1); c = c.sort(orderBy);
@Override @Nonnull public List<CollectionItem> list(@Nonnull CollectionId collectionId, int skip, int limit) { return datastore.getDB().getCollection("CollectionItemData") .find(new BasicDBObject(COLLECTION_ID, collectionId.getId()), new BasicDBObject(ITEM, 1)) // Requires a later version of mongo db - disable for now. // .setCollation(Collation.builder().locale("en").numericOrdering(true).build()) .limit(limit) .skip(skip) .toArray() .stream() .map(dbObject -> (String) dbObject.get(ITEM)) .map(CollectionItem::get) .collect(toList()); }