本文整理汇总了Java中com.mongodb.client.FindIterable类的典型用法代码示例。如果您正苦于以下问题:Java FindIterable类的具体用法?Java FindIterable怎么用?Java FindIterable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FindIterable类属于com.mongodb.client包,在下文中一共展示了FindIterable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAllDocuments
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* This method retrieve all the document(s)
*/
@Override
public void getAllDocuments() {
MongoDatabase db = null;
MongoCollection collection = null;
try {
db = client.getDatabase(mongo.getDataBase());
collection = db.getCollection(mongo.getSampleCollection());
FindIterable<Document> docs = collection.find(); //SELECT * FROM sample;
for (Document doc : docs) {
log.info(doc.getString("name"));
}
} catch (MongoException | ClassCastException e) {
log.error("Exception occurred while insert Value using **BasicDBObject** : " + e, e);
}
}
示例2: findReportsRaw
import com.mongodb.client.FindIterable; //导入依赖的package包/类
public static JSONObject findReportsRaw() {
JSONArray reports = new JSONArray();
FindIterable<Document> result = findReports();
result.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
JSONObject jsonReport = new JSONObject();
jsonReport.put("id", document.getObjectId("_id").toString());
jsonReport.put("comment", document.getString("comment"));
jsonReport.put("city_item_id", document.getString("city_item_id"));
jsonReport.put("priority", document.getInteger("priority"));
jsonReport.put("resolved", document.getBoolean("resolved"));
jsonReport.put("report_date", document.getLong("report_date"));
reports.put(jsonReport);
}
});
JSONObject jsonResults = new JSONObject();
jsonResults.put("reports", reports);
return jsonResults;
}
示例3: findOne
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* 查询一个
*
* @param collectionName 集合名
* @param query 查询条件
* @param fields 返回字段或者排除字段
* @param sort
* @return
*/
public Map<String, Object> findOne(
String collectionName,
MongodbQuery query, MongodbFields fields, MongodbSort sort) {
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find(query.getQuery());
if (fields == null) {
findIterable.projection(new MongodbFields().getDbObject());
} else {
findIterable.projection(fields.getDbObject());
}
if (sort != null) {
findIterable.sort(sort.getDbObject());
}
findIterable.limit(1);
MongoCursor<Document> cursor = findIterable.iterator();
try {
if (cursor.hasNext()) {
return cursor.next();
}
} finally {
cursor.close();
}
return null;
}
示例4: AddFlowerRatingReturnsTrueWithValidInput
import com.mongodb.client.FindIterable; //导入依赖的package包/类
@Test
public void AddFlowerRatingReturnsTrueWithValidInput() throws IOException{
assertTrue(plantController.addFlowerRating("16001.0", true, "first uploadId"));
MongoCollection plants = testDB.getCollection("plants");
FindIterable doc = plants.find(new Document().append("_id", new ObjectId("58d1c36efb0cac4e15afd202")));
Iterator iterator = doc.iterator();
Document result = (Document) iterator.next();
List<Document> ratings = (List<Document>) ((Document) result.get("metadata")).get("ratings");
assertEquals(1, ratings.size());
Document rating = ratings.get(0);
//assertTrue(rating.getBoolean("like"));
//assertEquals(new ObjectId("58d1c36efb0cac4e15afd202"),rating.get("ratingOnObjectOfId"));
}
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:18,代码来源:FlowerRating.java
示例5: doTranslate
import com.mongodb.client.FindIterable; //导入依赖的package包/类
private Map<String, List<String>> doTranslate(Set<String> tokens) {
MongoCollection<Document> lexColl = getLexCollection();
FindIterable<Document> lexs = lexColl.find(Filters.in(TERM_FIELD, tokens));
Map<String, List<String>> res = new HashMap<>();
for (Document doc : lexs) {
Document tr = (Document) doc.get(TRANSLATION_FIELD);
if (tr != null) {
tr.remove(NULL_VALUE);
res.put(doc.getString(TERM_FIELD), getRelevantTranslations((Map) tr));
}
}
return res;
}
示例6: failedInputOfComment
import com.mongodb.client.FindIterable; //导入依赖的package包/类
@Test
public void failedInputOfComment() throws IOException {
String json = "{ plantId: \"58d1c36efb0cac4e15afd27\", comment : \"Here is our comment for this test\" }";
assertFalse(plantController.addComment(json, "second uploadId"));
MongoClient mongoClient = new MongoClient();
MongoDatabase db = mongoClient.getDatabase(databaseName);
MongoCollection<Document> plants = db.getCollection("plants");
FindIterable findIterable = plants.find();
Iterator iterator = findIterable.iterator();
while(iterator.hasNext()){
Document plant = (Document) iterator.next();
List<Document> plantComments = (List<Document>) ((Document) plant.get("metadata")).get("comments");
assertEquals(0,plantComments.size());
}
}
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-dorfner-v2,代码行数:19,代码来源:TestPlantComment.java
示例7: findAndConsumer
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* 查询并逐条处理
*
* @param collectionName 集合名
* @param query 查询条件
* @param fields 返回字段或者排除字段
* @param sort 排序方式
* @param consumer 记录处理
* @return
*/
public void findAndConsumer(
String collectionName,
MongodbQuery query, MongodbFields fields,
MongodbSort sort, Consumer<Map<String, Object>> consumer) {
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find(query == null ? new Document() : query.getQuery());
if (fields == null) {
findIterable.projection(new MongodbFields().getDbObject());
} else {
findIterable.projection(fields.getDbObject());
}
if (sort != null) {
findIterable.sort(sort.getDbObject());
}
MongoCursor<Document> cursor = findIterable.iterator();
try {
while (cursor.hasNext()) {
Map<String, Object> document = cursor.next();
consumer.accept(document);
}
} finally {
cursor.close();
}
}
示例8: findAll
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* 查询
*
* @param clazz 类
* @param collectionName 集合名
* @param sort 排序
* @param <T>
* @return
*/
public <T> List<T> findAll(Class<T> clazz, String collectionName, MongodbSort sort) {
List<T> resultMapList = new ArrayList<T>();
MongoCollection<Document> collection = sMongoDatabase.getCollection(collectionName);
FindIterable<Document> findIterable = collection.find();
if(sort != null) {
findIterable.sort(sort.getDbObject());
}
MongoCursor<Document> cursor = findIterable.iterator();
try {
while (cursor.hasNext()) {
Document document = cursor.next();
T parseObject = JSON.parseObject(JSON.toJSONString(document), clazz);
resultMapList.add(parseObject);
}
} finally {
cursor.close();
}
return resultMapList;
}
示例9: addUserComment
import com.mongodb.client.FindIterable; //导入依赖的package包/类
public String addUserComment() {
MongoCollection collection = null;
Document query = null;
try {
// add a comment from user Melanie
String who = "Melanie";
Document comment = new Document("_id", who)
.append("name", who)
.append("address", new BasicDBObject("street", "123 Main Street")
.append("city", "Fastville")
.append("state", "MA")
.append("zip", 18180))
.append("comment", "MongoDB Is Web Scale");
// save the comment
collection = database.getCollection("comments");
collection.insertOne(comment);
// look up the comment from Melanie
query = new Document("_id", who);
FindIterable cursor = collection.find(query);
Object userComment = cursor.first();
return userComment.toString();
} finally {
collection.drop();
}
}
示例10: load
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* load the data by the query.
*
* @param <T>
* the subclass of Bean
* @param collection
* the collection name
* @param query
* the query
* @param b
* the Bean
* @return the Bean
*/
public <T extends Bean> T load(String collection, Bson query, T b) {
try {
MongoCollection<Document> db = getCollection(collection);
if (db != null) {
FindIterable<Document> d = db.find(query);
if (d != null) {
Document d1 = d.first();
if (d1 != null) {
b.load(d1, null);
return b;
}
}
}
} catch (Exception e) {
if (log.isErrorEnabled())
log.error(e.getMessage(), e);
}
return null;
}
示例11: getDataFromDatabase
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* Returns data from the database.
*
* @param searchKeys JSONObject containing the information regarding what data the return needs to contain.
* @return returns an JSONObject containing desired data.
*/
@Override
public synchronized JSONObject getDataFromDatabase (JSONObject searchKeys){
JSONArray retArray = new JSONArray();
JSONObject returnValue = new JSONObject();
BasicDBObject dbObject = (BasicDBObject) JSON.parse(searchKeys.toString());
FindIterable<Document> request = db.getCollection("requestTable").find(dbObject);
for(Document d: request){
retArray.put(new JSONObject(d.toJson()));
}
returnValue.put("dataArray", retArray);
return returnValue;
}
示例12: AddFlowerRatingReturnsTrueWithValidJsonInput
import com.mongodb.client.FindIterable; //导入依赖的package包/类
@Test
public void AddFlowerRatingReturnsTrueWithValidJsonInput() throws IOException{
String json = "{like: true, id: \"16001.0\"}";
assertTrue(plantController.addFlowerRating(json, "first uploadId"));
MongoCollection plants = testDB.getCollection("plants");
FindIterable doc = plants.find(new Document().append("_id", new ObjectId("58d1c36efb0cac4e15afd202")));
Iterator iterator = doc.iterator();
Document result = (Document) iterator.next();
List<Document> ratings = (List<Document>) ((Document) result.get("metadata")).get("ratings");
assertEquals(1, ratings.size());
Document rating = ratings.get(0);
assertTrue(rating.getBoolean("like"));
assertEquals(new ObjectId("58d1c36efb0cac4e15afd202"),rating.get("ratingOnObjectOfId"));
}
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:21,代码来源:FlowerRating.java
示例13: TestBedVisit
import com.mongodb.client.FindIterable; //导入依赖的package包/类
@Test
public void TestBedVisit(){
bedController.addBedVisit("10.0","first uploadId");
MongoCollection beds = testDB.getCollection("beds");
FindIterable doc = beds.find(new Document().append("_id", new ObjectId("58d1c36efb0cac4e15afd303")));
Iterator iterator = doc.iterator();
Document result = (Document) iterator.next();
//System.out.println(result);
List<Document> bedVisits = (List<Document>)((Document) result.get("metadata")).get("bedVisits");
//System.out.println(bedVisits);
assertEquals("",1 ,bedVisits.size());
Document visits = bedVisits.get(0);
//System.out.println(visits.get("visit"));
ObjectId objectId = new ObjectId();
String v = visits.get("visit").toString();
//checking to see that the type of visit is an of type/structure of ObjectID
assertEquals("they should both be of type org.bson.types.ObjectId ",objectId.getClass().getName(),visits.get("visit").getClass().getName());
assertEquals("the object id produced from a visit must be 24 characters",24,v.length());
}
开发者ID:UMM-CSci-3601-S17,项目名称:digital-display-garden-iteration-4-revolverenguardia-1,代码行数:26,代码来源:TestBedController.java
示例14: getServiceCountByOperatorId
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* 根据Operator订购的服务类型来查询可用数量(未过期的)
*
* @param serviceType service type
* @param operatorId operator ID
* @return number of service
*/
public long getServiceCountByOperatorId(Integer serviceType, ObjectId operatorId) {
long count = 0;
Document query = new Document();
query.append("user_id", operatorId);
query.append("serviceType", serviceType);
query.append("expired_at", new Document("$gte", new Date()));
FindIterable<Document> ds = this.database.getCollection("user_services").find(query);
if (ds != null) {
for (Document d : ds) {
if (d.getLong("used") != null) {
count += d.getLong("count") - d.getLong("used");
} else {
count += d.getLong("count");
}
}
}
return count;
}
示例15: findReportsForView
import com.mongodb.client.FindIterable; //导入依赖的package包/类
/**
* Returns all reports for view
*
* @return List
*/
public static List<ReportViewObject> findReportsForView() {
List<ReportViewObject> reports = new ArrayList<>();
FindIterable<Document> result = findReports();
result.forEach(new Block<Document>() {
@Override
public void apply(final Document document) {
Report.Builder b = new Report.Builder();
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String reportDate = df.format(new Date(document.getLong("report_date")));
reports.add(new ReportViewObject(
document.getObjectId("_id").toString(),
Integer.toString(document.getInteger("priority")),
document.getString("city_item_id"),
reportDate,
document.getBoolean("resolved") ? "YES" : "NO"));
}
});
return reports;
}