下面列出了怎么用com.mongodb.DBCursor的API类实例代码及写法,或者点击链接到github查看源代码。
private Map<String, Float> readDistanceScores(String startingObject, Set<String> others) {
Map<String, Float> result = new HashMap<String, Float>();
Query query = new org.springframework.data.mongodb.core.query.Query();
query.addCriteria(Criteria.where("type.name").is(_SIMILARITY_METHOD).orOperator(
Criteria.where("fromArtifact.$id").is(new ObjectId(startingObject)),
Criteria.where("toArtifact.$id").is(new ObjectId(startingObject))));
DBCollection dbCollection = mongoTemplate.getCollection("relation");
DBCursor cursor = dbCollection.find(query.getQueryObject());
List<DBObject> list = cursor.toArray();
for (DBObject dbObject : list) {
String toArtifact = ((DBRef) dbObject.get("toArtifact")).getId().toString();
String fromArtifact = ((DBRef) dbObject.get("fromArtifact")).getId().toString();
double value = ((double) dbObject.get("value"));
if (toArtifact.equals(startingObject)) {
if (others.contains(fromArtifact))
result.put(fromArtifact, (float) (1 - value));
} else if (others.contains(toArtifact))
result.put(toArtifact, (float) (1 - value));
}
return result;
}
@Override
public List<Content> getFeedFor(final User user, final int limit) {
List<Content> result = new ArrayList<Content>(limit);
DBCursor cursor = buckets.find(
findBy(BUCKET_OWNER_KEY, user.getUserId()), getFields(BUCKET_CONTENT_KEY)).
sort(sortByDecending(BUCKET_ID_KEY)).batchSize(config.bucket_read_batch_size);
try{
while(cursor.hasNext() && result.size() < limit){
DBObject currentBucket = cursor.next();
@SuppressWarnings("unchecked")
List<DBObject> contentList = (List<DBObject>)currentBucket.get(BUCKET_CONTENT_KEY);
int bucketSize = contentList.size();
for(int i = bucketSize - 1; i >= 0; --i){
result.add(new Content(contentList.get(i)));
if(result.size() >= limit)
break;
}
}
} finally {
cursor.close();
}
return result;
}
/**
* Find all DBObjects from database using this query.
* Note 1: it may do a full table scan if the query contains no index keys.
* Note 2: it will fetch all content into JVM memory rather than use lazy loading.
* So make sure you call it only at small collection such as configuration data.
*
* @param query
* @param databaseName
* @param namespace
* @param collection
* @param fields
* @return
*/
public static final List<DBObject> queryAllFromMongo(DBObject query,
String databaseName, String namespace, String collection,
DBObject fields, DBObject sortFields) {
DBCollection coll = getDBCollection(databaseName, namespace, collection);
//int count = (int)coll.count(query);
ArrayList<DBObject> objList = new ArrayList<DBObject>();
DBCursor list = coll.find(query, fields);
if ( sortFields != null ) {
list = list.sort(sortFields);
}
while ( list.hasNext() ) {
objList.add(list.next());
}
return objList;
}
protected final void extractBasicNode(String collectionName) {
int count = 0;
long startTime = System.currentTimeMillis();
logger.log(Level.INFO, "Building basic node for type: " + collectionName);
DBCursor cursor = mongo.getCollection(collectionName).find();
long recordCount = mongo.getCollection(collectionName).count();
while (cursor.hasNext()) {
DBObject item = cursor.next();
Vertex v = graph.addVertex(null);
logger.log(Level.FINE, "Adding vertex for {0}#{1} \t {2}",
new String[] { collectionName, (String) item.get("_id"), v.getId().toString() });
v.setProperty("mongoid", (String) item.get("_id"));
count++;
if (count % 200 == 0) {
logger.log(Level.FINE, "Importing {0} @ {1}", new String[] { collectionName, "" + count });
}
}
logger.log(Level.INFO, "\t RPS: {0}", (recordCount / (System.currentTimeMillis() - startTime)) * 1000);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/featureTagIndex/{product}/{major}.{minor}.{servicePack}/{build}")
public Response getFeatureTagIndexForReport(@BeanParam final Coordinates coordinates,
@QueryParam("searchText") final String searchText, @QueryParam("viewPassed") final Integer viewPassed,
@QueryParam("viewFailed") final Integer viewFailed,
@QueryParam("viewUndefined") final Integer viewUndefined, @QueryParam("viewSkipped") final Integer viewSkipped,
@QueryParam("start") final String start) {
final DBCollection featuresCollection = this.mongoLegacyDb.getCollection("features");
final BasicDBObject query = QueryBuilder.getInstance().buildFilterQuery(coordinates, searchText, viewPassed,
viewFailed, viewUndefined, viewSkipped, start);
query.append("$and", QueryBuilder.getInstance().buildHasTagsQuery());
final DBCursor results = featuresCollection.find(query,
new BasicDBObject("tags", 1).append("elements.tags", 1).append("name", 1).append("calculatedStatus", 1)
.append("id", 1).append("elements.steps", 1).append("elements.name", 1).append("elements.id", 1));
return Response.ok(SerializerUtil.serialise(getTagList(results))).build();
}
private Map<String, Float> readDistanceScores(String object) {
Map<String, Float> result = new HashMap<String, Float>();
Query query = new org.springframework.data.mongodb.core.query.Query();
query.addCriteria(Criteria.where("type.name").is(sm.getSimilarityName())
.orOperator(Criteria.where("fromArtifact.$id").is(new ObjectId(object)),
Criteria.where("toArtifact.$id").is(new ObjectId(object))));
DBCollection dbCollection = mongoTemplate.getCollection("relation");
DBCursor cursor = dbCollection.find(query.getQueryObject());
List<DBObject> list = cursor.toArray();
for (DBObject dbObject : list) {
String toArtifact = ((DBRef)dbObject.get("toArtifact")).getId().toString();
String fromArtifact = ((DBRef)dbObject.get("fromArtifact")).getId().toString();
double value = ((double)dbObject.get("value"));
if (toArtifact.equals(object))
result.put(fromArtifact, (float) (1 - value));
else
result.put(toArtifact, (float) (1 - value));
}
return result;
}
/**
* Execute a query on (mongo) database
*
* @param query path to query
* @param type type of data in query (string or json)
* @param collection collection in database
* @param modifications modifications to perform in query
*/
@When("^I execute a query '(.+?)' of type '(json|string)' in mongo '(.+?)' database using collection '(.+?)' with:$")
public void sendQueryOfType(String query, String type, String database, String collection, DataTable modifications) throws Exception {
try {
commonspec.setResultsType("mongo");
String retrievedData = commonspec.retrieveData(query, type);
String modifiedData = commonspec.modifyData(retrievedData, type, modifications);
commonspec.getMongoDBClient().connectToMongoDBDataBase(database);
DBCollection dbCollection = commonspec.getMongoDBClient().getMongoDBCollection(collection);
DBObject dbObject = (DBObject) JSON.parse(modifiedData);
DBCursor cursor = dbCollection.find(dbObject);
commonspec.setMongoResults(cursor);
} catch (Exception e) {
commonspec.getExceptions().add(e);
}
}
private ArrayNode getHistoricDocuments(DBCollection dbCollection, DBObject query) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode nodeArray = mapper.createArrayNode();
DBCursor cursor = dbCollection.find(query);
while(cursor.hasNext()) {
DBObject obj = cursor.next();
JsonNode json;
try {
json = mapper.readTree(obj.toString());
nodeArray.add(json);
} catch (IOException e) {
e.printStackTrace();
}
}
return nodeArray;
}
/**
* Find all DBObjects from database using this query.
* Note 1: it may do a full table scan if the query contains no index keys.
* Note 2: it will fetch all content into JVM memory rather than use lazy loading.
* So make sure you call it only at small collection such as configuration data.
*
* @param query
* @param databaseName
* @param namespace
* @param collection
* @param fields
* @return
*/
public static final List<DBObject> queryAllFromMongo(DBObject query,
String databaseName, String namespace, String collection,
DBObject fields, DBObject sortFields) {
DBCollection coll = getDBCollection(databaseName, namespace, collection);
int count = (int)coll.count(query);
ArrayList<DBObject> objList = new ArrayList<DBObject>();
DBCursor list = coll.find(query, fields);
if ( sortFields != null ) {
list = list.sort(sortFields);
}
while ( list.hasNext() ) {
objList.add(list.next());
}
return objList;
}
private JSONArray getJSONArrayFromDB(String db, String col) {
try {
JSONArray result = new JSONArray();
DBCollection collection = mongo.getDB(db).getCollectionFromString(col);
DBCursor cursor = collection.find();
while(cursor.hasNext()) {
DBObject obj = cursor.next();
JSONObject json = new JSONObject(JSON.serialize(obj));
result.put(json);
}
return result;
}
catch(Exception e) {
System.out.println("We got an error when creating a JSONArray: " + e);
return null;
}
}
private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget, MapReduceCommand.OutputType outputType, String orderBy, int pageNum, int pageSize, DBObject query) {
MapReduceOutput output = getCollection().mapReduce(map, reduce, outputTarget, outputType, query);
DBCollection c = output.getOutputCollection();
DBCursor cursor;
if(orderBy != null){
cursor = c.find().sort(SortUtil.getSort(orderBy)).skip((pageNum-1)*pageSize).limit(pageSize);
}else{
cursor = c.find().skip((pageNum-1)*pageSize).limit(pageSize);
}
List<DBObject> list = new ArrayList<>();
for(Iterator<DBObject> it = cursor.iterator(); it.hasNext(); ){
list.add(it.next());
}
cursor.close();
return list;
}
public List<Long> getRxnsWith(Long reactant, Long product) {
BasicDBObject query = new BasicDBObject();
query.put("enz_summary.products.pubchem", product);
query.put("enz_summary.substrates.pubchem", reactant);
DBCursor cur = this.dbReactions.find(query);
List<Long> reactions = new ArrayList<Long>();
while (cur.hasNext()) {
DBObject o = cur.next();
long id = (Integer) o.get("_id"); // checked: db type IS int
reactions.add(id);
}
cur.close();
return reactions;
}
private DBCursor createCursor() {
DBCollection oplog = _mongo.getDB("local").getCollection("oplog.rs");
BSONTimestamp startingTimestamp = getStartingTimestamp();
DBCursor cursor;
if (startingTimestamp == null) {
log.info("Tailing the oplog from the beginning...");
cursor = oplog.find();
} else {
log.info("Tailing the oplog from " + startingTimestamp);
BasicDBObject query = new BasicDBObject("ts", new BasicDBObject("$gt", startingTimestamp));
cursor = oplog.find(query);
cursor.addOption(Bytes.QUERYOPTION_OPLOGREPLAY);
}
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
cursor.addOption(Bytes.QUERYOPTION_TAILABLE);
cursor.addOption(Bytes.QUERYOPTION_AWAITDATA);
return cursor;
}
/**
* Find all DBObjects from database using this query.
* Note 1: it may do a full table scan if the query contains no index keys.
* Note 2: it will fetch all content into JVM memory rather than use lazy loading.
* So make sure you call it only at small collection such as configuration data.
*
* @param query
* @param databaseName
* @param namespace
* @param collection
* @param fields
* @return
*/
public static final List<DBObject> queryAllFromMongo(DBObject query,
String databaseName, String namespace, String collection,
DBObject fields, DBObject sortFields, int numToSkip, int limit) {
DBCollection coll = getDBCollection(databaseName, namespace, collection);
int count = (int)coll.count(query);
ArrayList<DBObject> objList = new ArrayList<DBObject>();
DBCursor list = coll.find(query, fields).skip(numToSkip).limit(limit);
if ( sortFields != null ) {
list = list.sort(sortFields);
}
while ( list.hasNext() ) {
objList.add(list.next());
}
return objList;
}
private List<DBObject> keywordInWaterfall(String in_field, String keyword) {
List<DBObject> waterfalls = new ArrayList<DBObject>();
BasicDBObject query = new BasicDBObject();
query.put(in_field, keyword);
BasicDBObject keys = new BasicDBObject();
DBCursor cur = this.dbWaterfalls.find(query, keys);
while (cur.hasNext()) {
DBObject o = cur.next();
waterfalls.add( convertDBObjectToWaterfall(o) );
}
cur.close();
return waterfalls;
}
public List<Long> getAllCollectionUUIDs(DBCollection collection) {
List<Long> ids = new ArrayList<Long>();
BasicDBObject query = new BasicDBObject();
BasicDBObject keys = new BasicDBObject();
keys.put("_id", 1); // 0 means exclude, rest are included
DBCursor cur = collection.find(query, keys);
while (cur.hasNext()) {
DBObject o = cur.next();
long uuid = (Integer)o.get("_id"); // checked: db type IS int
ids.add(uuid);
}
cur.close();
return ids;
}
public List<Seq> getSeqWithSARConstraints() {
List<Seq> seqs = new ArrayList<Seq>();
BasicDBObject query = new BasicDBObject();
query.put("sar_constraints", new BasicDBObject("$exists", true));
BasicDBObject keys = new BasicDBObject();
DBCursor cur = this.dbSeq.find(query, keys);
while (cur.hasNext()) {
DBObject o = cur.next();
seqs.add( convertDBObjectToSeq(o) );
}
cur.close();
return seqs;
}
public List<Seq> getSeqWithRxnRef(Long rxnId) {
List<Seq> seqs = new ArrayList<>();
BasicDBObject query = new BasicDBObject();
query.put("rxn_refs", rxnId);
DBCursor cur = this.dbSeq.find(query, new BasicDBObject());
try {
while (cur.hasNext()) {
DBObject o = cur.next();
seqs.add(convertDBObjectToSeq(o));
}
} finally {
if (cur != null) {
cur.close();
}
}
return seqs;
}
@Override
public void importCollection() {
DBCursor cursor = mongo.getCollection("studentSectionAssociation").find();
cursor.batchSize(BATCH_SIZE);
while (cursor.hasNext()) {
DBObject spa = cursor.next();
@SuppressWarnings("unchecked")
Map<String, Object> body = (Map<String, Object>) spa.get("body");
String studentId = (String) body.get("studentId");
String sectionId = (String) body.get("sectionId");
for (Vertex student : graph.getVertices("mongoid", studentId)) {
for (Vertex section : graph.getVertices("mongoid", sectionId)) {
Edge e = graph.addEdge(null, section, student, "studentSection");
e.setProperty("mongoid", spa.get("_id"));
if (body.containsKey("endDate")) {
e.setProperty("endDate", body.get("endDate"));
}
logger.log(Level.INFO, "Adding an edge between section: " + sectionId + " --> student: " + studentId);
}
}
}
}
private void toPrivateStorageList(String collectionName,
List<PrivateStorage> privatesStorage,
DBCursor cursor,
int pageNumber,
int pageSize) throws JsonProcessingException {
try {
cursor.skip(pageNumber);
cursor.limit(pageSize);
while (cursor.hasNext()) {
cursor.next();
String content = jsonParsingService.toJsonString(cursor.curr().toMap());
privatesStorage.add(PrivateStorage.builder()
.collectionName(collectionName)
.collectionContent(content)
.build());
}
} finally {
cursor.close();
}
}
@Override
public void importCollection() {
DBCursor cursor = mongo.getCollection("staffCohortAssociation").find();
while (cursor.hasNext()) {
DBObject spa = cursor.next();
Map<String, Object> body = (Map) spa.get("body");
List<String> staffIds = (List) body.get("staffId");
for (String staffId : staffIds) {
for (Vertex staff : graph.getVertices("mongoid", staffId)) {
List<String> cohortIds = (List) body.get("cohortId");
for (String cohortId : cohortIds)
for (Vertex program : graph.getVertices("mongoid", cohortId)) {
Edge e = graph.addEdge(null, program, staff, "staffCohort");
e.setProperty("mongoid", spa.get("_id"));
// This may not be safe.
e.setProperty("endDate", body.containsKey("endDate") ? body.get("endDate") : "");
e.setProperty("studentRecordAccess", body.get("studentRecordAccess"));
}
}
}
}
}
@Override
public List<GridFSDBFile> findAll(OrderBy orderBy, Page page) {
DBCursor _cursor = __dbCollection.find();
if (orderBy != null) {
_cursor.sort(orderBy.toBson());
}
if (page != null && page.page() > 0 && page.pageSize() > 0) {
_cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
}
List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
while (_cursor.hasNext()) {
_results.add((GridFSDBFile) _cursor.next());
}
_cursor.close();
return _results;
}
@Override
public List<GridFSDBFile> find(Query query, OrderBy orderBy, Page page) {
DBCursor _cursor = __dbCollection.find(query.toBson());
if (orderBy != null) {
_cursor.sort(orderBy.toBson());
}
if (page != null && page.page() > 0 && page.pageSize() > 0) {
_cursor.skip((page.page() - 1) * page.pageSize()).limit(page.pageSize());
}
List<GridFSDBFile> _results = new ArrayList<GridFSDBFile>();
while (_cursor.hasNext()) {
_results.add((GridFSDBFile) _cursor.next());
}
_cursor.close();
return _results;
}
@Override
public void importCollection() {
DBCursor cursor = mongo.getCollection("studentProgramAssociation").find();
while (cursor.hasNext()) {
DBObject spa = cursor.next();
Map<String, Object> body = (Map) spa.get("body");
for (Vertex student : graph.getVertices("mongoid", body.get("studentId"))) {
for (Vertex program : graph.getVertices("mongoid", body.get("programId"))) {
Edge e = graph.addEdge(null, program, student, "studentProgram");
e.setProperty("mongoid", spa.get("_id"));
// This may not be safe.
e.setProperty("endDate", body.get("endDate"));
}
}
}
}
public ShareJIDs(JID source, DBCursor cursor) {
this.jid = source.bare();
try (DBCursor iterator = cursor) {
while (iterator.hasNext()) {
this.resources.add(MongoUtils.asString(iterator.next(), Dictionary.FIELD_RESOURCE));
}
}
}
private Timeout timeout() {
try (DBCursor cursor = BridgeExchangerContext.this.config.collection().find(BasicDBObjectBuilder.start(BridgeExchangerContext.this.date, BasicDBObjectBuilder.start("$lt", System.currentTimeMillis() - BridgeExchangerContext.this.timeout).get()).get(), BridgeExchangerContext.this.filter)) {
while (cursor.hasNext()) {
Exchanger exchanger = BridgeExchangerContext.this.activate(MongoUtils.asString(DBObject.class.cast(cursor.next()), Dictionary.FIELD_HOST));
// Double check 4 multi thread
if (exchanger != null) {
BridgeExchangerContext.this.log.warn("Timeout socks: " + exchanger.host());
exchanger.close(Terminal.ALL);
}
}
}
return this;
}
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;
}
@SuppressWarnings("unchecked")
private <V> V getValueImpl(String name, V defaultVal, Class<V> type) {
BasicDBObject find = new BasicDBObject("nation", nation);
BasicDBObject query = new BasicDBObject(name, 1);
try (DBCursor cursor = this.users.find(find, query)) {
if (cursor.hasNext()) {
DBObject result = cursor.next();
Object obj = result.get(name);
//Auto-magically convert any strings to numbers, if we requested a number type
if (obj instanceof String && Number.class.isAssignableFrom(type)) {
try {
double val = Double.parseDouble((String)obj);
if (type == Double.class) {
return (V) Double.valueOf(val);
} else if (type == Float.class) {
return (V) Float.valueOf((float)val);
} else if (type == Integer.class) {
return (V) Integer.valueOf((int)val);
} else if (type == Long.class) {
return (V) Long.valueOf((long)val);
}
} catch (NumberFormatException e) {
return defaultVal;
}
} else if (obj instanceof String && Boolean.class.isAssignableFrom(type)) {
return (V) Boolean.valueOf("true".equalsIgnoreCase((String)obj));
}
return type.cast(obj);
}
}
return defaultVal;
}
@Test
public void testUpdate() {
DBCollection coll = createMockCollection();
DBCursor cursor = createMockCursor();
DBObject query = new BasicDBObject("_id", new ObjectId("aaaaaaaaaaaaaaaaaaaaaaa2"));
DBObject dbObj = new BasicDBObject();
dbObj.put("_id", new ObjectId("aaaaaaaaaaaaaaaaaaaaaaa2"));
dbObj.put("title", "new title");
dbObj.put("completed", true);
dbObj.put("order", 0);
expect(cursor.next()).andReturn(dbObj);
cursor.close();
expectLastCall();
replay(cursor);
expect(coll.find(eq(query))).andReturn(cursor);
ToDo newTd = new ToDo();
newTd.setId("aaaaaaaaaaaaaaaaaaaaaaa2");
newTd.setCompleted(true);
newTd.setTitle("new title");
newTd.setOrder(0);
expect(coll.update(eq(query), eq(dbObj))).andReturn(null);
replay(coll);
MongoStore store = new MongoStore(coll);
assertEquals(newTd, store.update("aaaaaaaaaaaaaaaaaaaaaaa2", newTd));
verify(cursor);
verify(coll);
}
public List<Reaction> getRxnsWithAll(List<Long> reactants, List<Long> products) {
if (reactants.size() == 0 && products.size() == 0) {
throw new IllegalArgumentException("Reactants and products both empty! Query would return entire DB.");
}
BasicDBObject query = new BasicDBObject();
if (!reactants.isEmpty()) {
BasicDBList substrateIds = new BasicDBList();
substrateIds.addAll(reactants);
query.put("enz_summary.substrates.pubchem", new BasicDBObject("$all", substrateIds));
}
if (!products.isEmpty()) {
BasicDBList productIds = new BasicDBList();
productIds.addAll(products);
query.put("enz_summary.products.pubchem", new BasicDBObject("$all", productIds));
}
DBCursor cur = this.dbReactions.find(query);
List<Reaction> reactions = new ArrayList<Reaction>();
try {
while (cur.hasNext()) {
DBObject o = cur.next();
reactions.add(convertDBObjectToReaction(o));
}
} finally {
cur.close();
}
return reactions;
}