/** * 按条件查询出单个对象 * * @param tableName * @param timeValue * @param appId * @param appVersion * @param appChannel * @param appPlatform * @param tenantId * @param network * @param carrier * @return */ public Document findOneBy( String tableName, String timeValue, String appId, String appVersion, String appChannel, String appPlatform, String network, String carrier, String tenantId) { MongoDatabase db = mongoClient.getDatabase(appId); Document matchFields = new Document(); matchFields.put("timeValue", timeValue); matchFields.put("appVersion", appVersion); matchFields.put("appChannel", appChannel); matchFields.put("appPlatform", appPlatform); matchFields.put("network", network); matchFields.put("carrier", carrier); if (tenantId != null && !tenantId.trim().isEmpty()) { matchFields.put("tenantId", tenantId); } else { // 当相同的条件下有tenantId对应一条数据,无tenantId对应一条数据,所以要加上matchFields.put("tenantId", // null);,以免查询出两条数据,会照成结果错误 matchFields.put("tenantId", null); } FindIterable<Document> iterable = db.getCollection(tableName).find(matchFields); if (iterable.iterator().hasNext()) { return iterable.first(); } else { return null; } }
public Document findBy(String appId, String deviceId) { MongoDatabase db = mongoClient.getDatabase(appId); FindIterable<Document> iterable = db.getCollection("Widget").find(new Document("deviceId", deviceId)); if (iterable.iterator().hasNext()) { return iterable.first(); } else { return null; } }
@Override public SensorTypeInfo read(SensorType id) { SensorTypeInfo zettaDriver = new SensorTypeInfo(); zettaDriver.setType(id.getCode()); zettaDriver.setTypeName(id.getName()); MongoCollection<Document> collection = CloudMongoDB.getInstance().getCollection("Drivers"); // finds in database FindIterable<Document> users = collection.find(new Document("type", id.getCode())); SensorTypeInfo sensorInfo = SensorTypeInfoInstanceCreator.createInstance(users.first()); // System.out.println(sensorInfo); return sensorInfo; }
/** Read user account from DB */ public static UserAccount readFromDb(String userId) { if (!useMongoDB) { if (virtualDB.containsKey(userId)) { return virtualDB.get(userId); } return null; } if (db == null) { try { connect(); } catch (ParseException e) { e.printStackTrace(); } } FindIterable<Document> iterable = db.getCollection("user_accounts").find(new Document("userID", userId)); if (iterable == null) { return null; } Document doc = iterable.first(); return new UserAccount(userId, doc.getString("userName"), doc.getString("additionalInfo")); }
public static Document meepDocumentBuilder(JsonObject arg, MongoCollection<Document> meepCol) { Document meep = new Document(); if (arg.get("coordinates").isJsonNull()) return null; double longi = arg.getAsJsonObject("coordinates").getAsJsonArray("coordinates").get(0).getAsDouble(); double lat = arg.getAsJsonObject("coordinates").getAsJsonArray("coordinates").get(1).getAsDouble(); BasicDBObject aux = getCloseMeepsQuery(lat, longi); FindIterable<Document> auxCol = meepCol.find(aux); if (auxCol.first() != null) { System.out.println("Received valid tweet but too close to another"); return null; } meep.append( "senderName", arg.getAsJsonObject("user").getAsJsonPrimitive("screen_name").getAsString()); System.out.println( "Received valid tweet at " + lat + "," + longi + " posted by " + meep.getString("senderName")); meep.append("senderId", arg.getAsJsonObject("user").getAsJsonPrimitive("id").getAsInt()); meep.append( "twitterUserPicture", arg.getAsJsonObject("user").getAsJsonPrimitive("profile_image_url").getAsString()); meep.append("message", arg.getAsJsonPrimitive("text").getAsString()); meep.append("type", "tweet"); meep.append("isRoot", true); meep.append("commentCounter", 0); meep.append("likeCounter", 0); meep.append("viewCounter", 0); /*if(arg.getAsJsonObject("entities").has("media")) meep.append("picture", arg.getAsJsonObject("entities").getAsJsonObject("media").get("media_url").getAsString()); else*/ meep.append("picture", null); BasicDBList list = new BasicDBList(); list.add(longi); list.add(lat); Document jobj = new Document(); jobj.append("type", "Point"); jobj.append("coordinates", list); meep.append("location", jobj); BasicDBList comments = new BasicDBList(); meep.append("comments", comments); BasicDBList receipts = new BasicDBList(); meep.append("receipts", receipts); BasicDBList registrees = new BasicDBList(); meep.append("registrees", registrees); BasicDBList likes = new BasicDBList(); meep.append("likes", likes); meep.append("categoryId", Category.TWITTER.getId()); BasicDBList hashtags = new BasicDBList(); HashtagController controller = new HashtagController(); LinkedList<String> aux2 = controller.extractHashtags(arg.getAsJsonPrimitive("text").getAsString()); for (String s : aux2) hashtags.add(s); meep.append("hashtags", hashtags); meep.append("isPublic", true); return meep; }