private boolean isReplicaSetStarted(BasicDBObject setting) { if (setting.get("members") == null) { return false; } BasicDBList members = (BasicDBList) setting.get("members"); for (Object m : members.toArray()) { BasicDBObject member = (BasicDBObject) m; logger.info(member.toString()); int state = member.getInt("state"); logger.info("state: {}", state); // 1 - PRIMARY, 2 - SECONDARY, 7 - ARBITER if (state != 1 && state != 2 && state != 7) { return false; } } return true; }
@Test public void shouldCreateCorrectNearQueryWithoutMaxDistance() { // given double latitude = 3.2; double longitude = 5.7; QueryImpl<Object> stubQuery = (QueryImpl<Object>) getDs().find(Object.class); stubQuery.disableValidation(); Geo2dSphereCriteria criteria = Geo2dSphereCriteria.geo(stubQuery, "location", NEAR, pointBuilder() .latitude(latitude) .longitude(longitude) .build()); // when BasicDBObject queryDocument = new BasicDBObject(); criteria.addTo(queryDocument); // then assertThat(queryDocument.toString(), JSONMatcher.jsonEqual(" { location : " + " { $near : " + " { $geometry : " + " { type : 'Point' , " + " coordinates : [ " + longitude + " , " + latitude + "]" + " } " + " }" + " }" + "}")); } }
@Test public void shouldCreateCorrectNearQueryWithMaxDistance() { // given double maxDistanceMeters = 13; double latitude = 3.2; double longitude = 5.7; QueryImpl<Object> stubQuery = (QueryImpl<Object>) getDs().find(Object.class); stubQuery.disableValidation(); Geo2dSphereCriteria criteria = Geo2dSphereCriteria.geo(stubQuery, "location", NEAR, pointBuilder() .latitude(latitude) .longitude(longitude) .build()) .maxDistance(maxDistanceMeters); // when BasicDBObject queryDocument = new BasicDBObject(); criteria.addTo(queryDocument); // then assertThat(queryDocument.toString(), JSONMatcher.jsonEqual(" { location : " + " { $near : " + " { $geometry : " + " { type : 'Point' , " + " coordinates : [ " + longitude + " , " + latitude + "]" + " }, " + " $maxDistance : " + maxDistanceMeters + " }" + " }" + "}")); }
log.debug("Fetching data from " + documentName + " for Filter " + mongoQuery.toString());
@Override public String toString() { return filterObject.toString(); } }
public String toString() { return dbObject.toString(); } }
@Override public String toString() { if (this.basicObj == null) { return ""; } return this.basicObj.toString(); }
@Override public String toString() { if (this.basicObj == null) { return ""; } return this.basicObj.toString(); }
@Override public String toString() { if (this.basicObj == null) { return ""; } return this.basicObj.toString(); }
@Override public String toString() { return options.toString(); } }
@Override public String toString() { return options.toString(); } }
@Override public BasicDBObject getInstruction() throws TranslatorException { LogManager.logDetail(LogConstants.CTX_CONNECTOR, "{\"$project\": {"+this.projection.toString()+"}}"); //$NON-NLS-1$ //$NON-NLS-2$ return new BasicDBObject("$project", this.projection); //$NON-NLS-1$ } }
protected void writeObjectToSummaryFile(String collectionName, BasicDBObject comment) throws IOException { writeToSummaryFile(collectionName, comment.toString()); }
// Define the search query BasicDBObject searchQuery = new BasicDBObject().append("idU", idUser); // To create the json query to modify BasicDBObject logDocument = new BasicDBObject(); // Create the object and add it to a list (because the $each require a list) List<DBObject> list = new ArrayList<DBObject>(); DBObject object = new BasicDBObject().append("text", logMessage.getText()); object.append("level", logMessage.getLevel()) object.append("date", logMessage.getDate()); list.add(object); // Create the $sort and $slice query at the same time logDocument.append( "$push", new BasicDBObject().append("logs", new BasicDBObject().append("$each", list).append("$sort", new BasicDBObject().append("date", 1)) .append("$slice", -10))); String json = "{findAndModify:\"collectionLog\", query:" + searchQuery.toString() + ", update: " + logDocument.toString() + ", upsert:true}"; try { getMongoTemplate().executeCommand(json); } catch (Exception e) { System.out.println(e); }
/** * Convert to JSON string. * @param obj * @return */ public static String toJsonString(Object obj){ if(obj == null){ return null; } BasicDBObject bdbo; if(obj instanceof DBObject){ bdbo = (BasicDBObject)obj; }else{ DBObject dbo = MapperUtil.toDBObject(obj, true); bdbo = (BasicDBObject)dbo; } return bdbo.toString(); }
BasicDBObject basicDBObject = new BasicDBObject(); basicDBObject.put("company", "stackoverflow"); basicDBObject.append("established", "when I started coding"); basicDBObject.append("employees", Arrays.<DBObject>asList( new BasicDBObject("firstName", "john") .append("lastName", "doe"), new BasicDBObject("firstName", "anna") .append("lastName", "smith"), new BasicDBObject("firstName", "peter") .append("lastName", "jones") )); System.out.println(basicDBObject.toString());
DBCollection userCollection = db.getCollection("collection"); BasicDBObject orQuery = new BasicDBObject(); List<BasicDBObject> obj1 = new ArrayList<BasicDBObject>(); obj1.add(new BasicDBObject("a", "text")); obj1.add(new BasicDBObject("b", "text")); orQuery.put("$or", obj1); BasicDBObject andQuery = new BasicDBObject(); List<BasicDBObject> obj = new ArrayList<BasicDBObject>(); obj.add(new BasicDBObject("c", "text")); obj.add(orQuery); andQuery.put("$and", obj); System.out.println(andQuery.toString()); DBCursor cursor = userCollection.find(andQuery); while (cursor.hasNext()) { System.out.println(cursor.next()); }
protected void writeIndexInfoToSummaryFile(String collectionName, BasicDBObject index) throws IOException { BasicDBObject i = (BasicDBObject)index.get("key"); // don't write the _id index if(!i.containsField("_id")){ writeToSummaryFile(collectionName, i.toString()); } } }
private boolean isReplicaSetStarted(BasicDBObject setting) { if (setting.get("members") == null) { return false; } BasicDBList members = (BasicDBList) setting.get("members"); for (Object m : members.toArray()) { BasicDBObject member = (BasicDBObject) m; logger.info(member.toString()); int state = member.getInt("state"); logger.info("state: {}", state); // 1 - PRIMARY, 2 - SECONDARY, 7 - ARBITER if (state != 1 && state != 2 && state != 7) { return false; } } return true; }
protected void traceIfRequired(String collection, Watch w) { if (mongo.tracing && w.elapsedMillis() >= mongo.traceLimit) { String location = determineLocation(); Doc explanation = explain(collection); mongo.traceData.put(location, Tuple.create(collection + ": " + filterObject.toString() + " [" + w.duration() + "]", explanation.toString())); } if (w.elapsedMillis() > mongo.getLogQueryThresholdMillis()) { mongo.numSlowQueries.inc(); DB.SLOW_DB_LOG.INFO("A slow MongoDB query was executed (%s): %s\n%s\n%s", w.duration(), collection, filterObject, ExecutionPoint.snapshot().toString()); } }