当前位置: 首页>>代码示例>>Java>>正文


Java DBCollection.find方法代码示例

本文整理汇总了Java中com.mongodb.DBCollection.find方法的典型用法代码示例。如果您正苦于以下问题:Java DBCollection.find方法的具体用法?Java DBCollection.find怎么用?Java DBCollection.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.mongodb.DBCollection的用法示例。


在下文中一共展示了DBCollection.find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: updateUserChangeNameAndSurnametoName

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
 * update 6: {@link UserDocument} has changed; 'name' and 'surname' will be concat to 'name'.
 * for each every user document get 'name' and 'surname', concat them, update 'name', remove field surname and update document.
 *
 * @since V7
 */
@ChangeSet(order = "008", id = "updateUserChangeNameAndSurnameToName", author = "admin")
public void updateUserChangeNameAndSurnametoName(final MongoTemplate template) {
  final DBCollection userCollection = template.getCollection("user");

  final Iterator<DBObject> cursor = userCollection.find();
  while (cursor.hasNext()) {
    final DBObject current = cursor.next();

    final Object nameObj = current.get("name");
    final Object surnameObj = current.get("surname");
    final String updateName = (nameObj != null ? nameObj.toString() : "") + " " + (surnameObj != null ? surnameObj.toString() : "");

    final BasicDBObject updateQuery = new BasicDBObject();
    updateQuery.append("$set", new BasicDBObject("name", updateName));
    updateQuery.append("$unset", new BasicDBObject("surname", ""));

    final BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("_id", current.get("_id"));

    userCollection.update(searchQuery, updateQuery);
  }
}
 
开发者ID:nkolytschew,项目名称:mongobee_migration_example,代码行数:29,代码来源:DatabaseChangeLog.java

示例2: afterCreate

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/** Read the occasions that are stored in the database and schedule them to run. */
@PostConstruct
public void afterCreate() {
  String method = "afterCreate";
  logger.entering(clazz, method);

  orchestrator.setOccasionResource(this);

  DBCollection occasions = getCollection();
  DBCursor cursor = occasions.find();
  while (cursor.hasNext()) {
    DBObject dbOccasion = cursor.next();
    Occasion occasion = new Occasion(dbOccasion);
    try {
      // TODO: There was a comment here about how we should mark the event as
      //       popped in the database, which will have different meaning for
      //       one-time or interval occasions.  Need to re-visit this.
      orchestrator.scheduleOccasion(occasion);
    } catch (Throwable t) {
      logger.log(Level.WARNING, "Could not schedule occasion at startup", t);
    }
  }

  logger.exiting(clazz, method);
}
 
开发者ID:OpenLiberty,项目名称:sample-acmegifts,代码行数:26,代码来源:OccasionResource.java

示例3: generateFile

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public void generateFile(String filename) {
    DB db = MongoHelper.mongoMerchantDB();

    DBCollection col = db.getCollection(COLLECTION_SYNONYMS);
    DBCursor cursor = col.find();
    try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)))) {
        while (cursor.hasNext()) {
            DBObject doc = cursor.next();
            String word = doc.get(FIELD_KEY_WORLD) != null ? doc.get(FIELD_KEY_WORLD).toString() : null;
            String synonyms = doc.get(FIELD_KEY_WORLD) != null
                ? StringUtils.join((BasicDBList) doc.get(FIELD_KEY_SYNONYMS), ",") : null;
            if (word != null && synonyms != null) {
                out.println(createLine(word, synonyms));
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("IOException: Current db cursor with id: " + cursor.curr().get("_id"), e);
    }
}
 
开发者ID:geetools,项目名称:geeCommerce-Java-Shop-Software-and-PIM,代码行数:20,代码来源:SynonymsGenerator.java

示例4: getCacheEntry

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public CacheEntry getCacheEntry(String key, CacheEntry defaultValue) {
	DBCursor cur = null;
	DBCollection coll = getCollection();
	BasicDBObject query = new BasicDBObject("key", key.toLowerCase());

	// be sure to flush
	flushInvalid(coll,query);

	cur = coll.find(query);

	if (cur.count() > 0) {
		hits++;
		MongoDBCacheDocument doc = new MongoDBCacheDocument((BasicDBObject) cur.next());
		doc.addHit();
		//update the statistic and persist
		save(doc,0);
		return new MongoDBCacheEntry(doc);
	}
	misses++;
	return defaultValue;
}
 
开发者ID:lucee,项目名称:extension-mongodb,代码行数:22,代码来源:MongoDBCache.java

示例5: getMongoDBData

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public StringBuilder getMongoDBData() {
	DBCollection collection = MongoUtil.getCollection("some_db",
			"some_collection");

	DBCursor cursor = collection.find();
	StringBuilder data = new StringBuilder();

	long startTime = System.currentTimeMillis();
	while (cursor.hasNext()) {
		data.append(cursor.next());
	}
	long endTime = System.currentTimeMillis();

	System.out.println("Time taken : " + (endTime - startTime));

	return data;
}
 
开发者ID:JitenderChahar,项目名称:JerseyRestBoilerplate,代码行数:18,代码来源:EmployeeDao.java

示例6: doQuery

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
 * Perform a single 'query' operation on the local object store.
 *
 * @param template  Query template indicating what objects are sought.
 * @param collection  Collection to query.
 * @param maxResults  Maximum number of result objects to return, or 0 to
 *    indicate no fixed limit.
 *
 * @return a list of ObjectDesc objects for objects matching the query.
 */
private List<ObjectDesc> doQuery(JSONObject template,
                                 DBCollection collection, int maxResults) {
    List<ObjectDesc> results = new LinkedList<ObjectDesc>();

    try {
        DBObject query = jsonObjectToDBObject(template);
        DBCursor cursor;
        if (maxResults > 0) {
            cursor = collection.find(query, null, 0, -maxResults);
        } else {
            cursor = collection.find(query);
        }
        for (DBObject dbObj : cursor) {
            JSONObject jsonObj = dbObjectToJSONObject(dbObj);
            String obj = jsonObj.sendableString();
            results.add(new ObjectDesc("query", obj, null));
        }
    } catch (Exception e) {
        results.add(new ObjectDesc("query", null, e.getMessage()));
    }
    return results;
}
 
开发者ID:FUDCo,项目名称:Elko,代码行数:33,代码来源:MongoObjectStore.java

示例7: getAllUsers

import com.mongodb.DBCollection; //导入方法依赖的package包/类
/**
 * Get all user profiles.
 *
 * @return All user profiles (excluding private fields like password).
 */
@GET
@Produces("application/json")
public Response getAllUsers() {
  // Validate the JWT. The JWT must be in the 'users' group.
  try {
    validateJWT(new HashSet<String>(Arrays.asList("users")));
  } catch (JWTException jwte) {
    return Response.status(Status.UNAUTHORIZED)
        .type(MediaType.TEXT_PLAIN)
        .entity(jwte.getMessage())
        .build();
  }

  // Get all the users from the database, and add them to an array.
  DB database = mongo.getMongoDB();
  DBCollection dbCollection = database.getCollection(User.DB_COLLECTION_NAME);
  DBCursor cursor = dbCollection.find();
  JsonArrayBuilder userArray = Json.createArrayBuilder();
  while (cursor.hasNext()) {
    // Exclude all private information from the list.
    userArray.add((new User(cursor.next()).getPublicJsonObject()));
  }

  // Return the user list to the caller.
  JsonObjectBuilder responseBuilder = Json.createObjectBuilder().add("users", userArray.build());
  return Response.ok(responseBuilder.build(), MediaType.APPLICATION_JSON).build();
}
 
开发者ID:OpenLiberty,项目名称:sample-acmegifts,代码行数:33,代码来源:UserResource.java

示例8: selectSet

import com.mongodb.DBCollection; //导入方法依赖的package包/类
public DBCursor selectSet(DBCollection collection) {
	DBObject fields = getFields();
	DBObject query = getQuery();
	DBObject orderByObject = getOrderByObject();
	DBCursor cursor = null;

	// 日志
	log(fields, query, orderByObject);

	if (null != query && null == fields) {
		cursor = collection.find(query);
	} else if (null == query && null != fields) {
		cursor = collection.find(new BasicDBObject(), fields);
	} else if (null != fields && null != query) {
		cursor = collection.find(query, fields);
	} else {
		cursor = collection.find();
	}

	if (null != orderByObject) {
		cursor.sort(orderByObject);
	}
	if (null != this.limit) {
		if (null == this.limit.getOffset()) {
			cursor.limit(this.limit.getRowCount());
		} else {
			cursor.limit(this.limit.getRowCount());
			cursor.skip(this.limit.getOffset());
		}
	}
	return cursor;
}
 
开发者ID:xsonorg,项目名称:tangyuan2,代码行数:33,代码来源:SelectVo.java

示例9: contains

import com.mongodb.DBCollection; //导入方法依赖的package包/类
@Override
public boolean contains(String key) {
	DBCollection coll = getCollection();
	BasicDBObject query = new BasicDBObject();
	query.put("key", key.toLowerCase());
	DBCursor cur = coll.find(query);
	return cur.count() > 0;
}
 
开发者ID:lucee,项目名称:extension-mongodb,代码行数:9,代码来源:MongoDBCache.java

示例10: remove

import com.mongodb.DBCollection; //导入方法依赖的package包/类
@Override
public boolean remove(String key) {
	DBCollection coll = getCollection();
	BasicDBObject query = new BasicDBObject();
	query.put("key", key.toLowerCase());
	DBCursor cur = coll.find(query);
	if (cur.hasNext()) {
		doDelete(cur.next());
		return true;
	}
	return false;
}
 
开发者ID:lucee,项目名称:extension-mongodb,代码行数:13,代码来源:MongoDBCache.java

示例11: qAll_Keys

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private DBCursor qAll_Keys() {
	DBCollection coll = getCollection();
	Integer attempts = 0;
	DBCursor cur = null;
	//get all entries but retrieve just the keys for better performance
	cur = coll.find(new BasicDBObject(), new BasicDBObject("key", 1));
	return cur;
}
 
开发者ID:lucee,项目名称:extension-mongodb,代码行数:9,代码来源:MongoDBCache.java

示例12: qAll_Values

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private DBCursor qAll_Values() {
	DBCollection coll = getCollection();
	DBCursor cur = null;
	//get all entries but retrieve just the keys for better performance
	cur = coll.find(new BasicDBObject(), new BasicDBObject("data", 1));
	return cur;
}
 
开发者ID:lucee,项目名称:extension-mongodb,代码行数:8,代码来源:MongoDBCache.java

示例13: qAll_Keys_Values

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private DBCursor qAll_Keys_Values() {
	DBCollection coll = getCollection();
	DBCursor cur = null;

	//get all entries but retrieve just the keys for better performance
	cur = coll.find(new BasicDBObject(), new BasicDBObject("key", 1).append("data", 1));
	return cur;
}
 
开发者ID:lucee,项目名称:extension-mongodb,代码行数:9,代码来源:MongoDBCache.java

示例14: find

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private static ModulesContainer find(DBObject query, DBObject orderBy, Integer limit, Integer page) throws SinfonierException {
  DBCollection collection = MongoFactory.getDB().getCollection(collectionName);
  List<Module> list = new ArrayList<Module>();
  DBCursor cursor;
  
  if (limit != null) {
    page = (page != null && page > 0) ? ((page-1)*limit) : 0;
  } else {
    page = null;
  }

  if (query == null) {
    cursor = collection.find();
  } else {
    cursor = collection.find(query);
  }

  int totalModules = cursor.count();
  
  if (orderBy != null) {
    cursor = cursor.sort(orderBy);
  }
  
  if (page != null && page > 0) {
    cursor.skip(page);
  }
  
  if (limit != null) {
    cursor = cursor.limit(limit);
  }

  for (DBObject dbObject : cursor) {
    list.add(new Module(dbObject));
  }

  return new ModulesContainer(list, totalModules);
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:38,代码来源:Module.java

示例15: find

import com.mongodb.DBCollection; //导入方法依赖的package包/类
private static TopologiesContainer find(DBObject query, DBObject orderBy, Integer limit, Integer page) throws SinfonierException {
  DBCollection collection = MongoFactory.getDB().getCollection(getCollectionName());
  List<Topology> list = new ArrayList<Topology>();
  DBCursor cursor;

  if (limit != null) {
    page = (page != null && page > 0) ? ((page-1)*limit) : 0;
  } else {
    page = null;
  }
  
  if (query == null) {
    cursor = collection.find();
  } else {
    cursor = collection.find(query);
  }
  
  int totalTopologies = cursor.count();

  if (orderBy != null) {
    cursor = cursor.sort(orderBy);
  }

  if (page != null && page > 0) {
    cursor.skip(page);
  }
  
  if (limit != null) {
    cursor = cursor.limit(limit);
  }

  for (DBObject dbObject : cursor) {
    list.add(new Topology(dbObject));
  }

  return new TopologiesContainer(list, totalTopologies);
}
 
开发者ID:telefonicaid,项目名称:fiware-sinfonier,代码行数:38,代码来源:Topology.java


注:本文中的com.mongodb.DBCollection.find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。