Java Code Examples for com.mongodb.DBCursor#next()

The following examples show how to use com.mongodb.DBCursor#next() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StudentCohortEdgeImporter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Override
public void importCollection() {
    DBCursor cursor = mongo.getCollection("studentCohortAssociation").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("cohortId"))) {
                Edge e = graph.addEdge(null, program, student, "studentCohort");
                e.setProperty("mongoid", spa.get("_id"));
                // This may not be safe.
                e.setProperty("endDate", body.get("endDate"));
            }
            
        }
        
    }
    
}
 
Example 2
Source File: PrivateStorageRepository.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
private void toPrivateStorageList(String collectionName, List<PrivateStorage> privatesStorage, DBCursor cursor) throws JsonProcessingException {
    try {
        while (cursor.hasNext()) {
            cursor.next();

            String content = jsonParsingService.toJsonString(cursor.curr().toMap());

            privatesStorage.add(PrivateStorage.builder()
                    .collectionName(collectionName)
                    .collectionContent(content)
                    .build());
        }
    } finally {
        cursor.close();
    }
}
 
Example 3
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public List<Seq> getSeqFromSeqEcOrg(String seq, String ec, String organism) {
  List<Seq> seqs = new ArrayList<>();
  BasicDBObject query = new BasicDBObject();
  query.put("seq", seq);
  query.put("ecnum", ec);
  query.put("org", organism);

  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;
}
 
Example 4
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 6
Source File: StudentSectionEdgeImporter.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@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);
            }
        }            
    }        
}
 
Example 7
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private List<Reaction> keywordInReaction(String in_field, String keyword) {
  List<Reaction> rxns = new ArrayList<Reaction>();
  BasicDBObject query = new BasicDBObject();
  query.put(in_field, keyword);

  BasicDBObject keys = new BasicDBObject();

  DBCursor cur = this.dbReactions.find(query, keys);
  while (cur.hasNext()) {
    DBObject o = cur.next();
    rxns.add( convertDBObjectToReaction(o) );
  }
  cur.close();

  return rxns;
}
 
Example 8
Source File: FanoutOnWriteSizedBuckets.java    From socialite with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 9
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
public Map<String, Long> constructAllInChIs() {
  Map<String, Long> chems = new HashMap<String, Long>();
  BasicDBObject keys = new BasicDBObject();
  keys.append("_id", true);
  keys.append("InChI", true);
  DBCursor cur = constructCursorForMatchingChemicals(null, null, keys);
  while (cur.hasNext()) {
    DBObject o = cur.next();
    long uuid = (Long)o.get("_id"); // checked: db type IS long
    String inchi = (String)o.get("InChI");
    chems.put(inchi, uuid);
  }

  cur.close();
  return chems;
}
 
Example 10
Source File: UpdateMongePmids.java    From bluima with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		MongoConnection mongo = new MongoConnection(MONGO_FT_CONNECTION);

		DBCursor it = mongo.coll.find().batchSize(1000);

		int i = 0;
		while (it.hasNext()) {
			DBObject dbObject = (DBObject) it.next();
			int pmId = Integer.parseInt(dbObject.get("_id").toString());
			// dbObject.put(MongoFieldMapping.PM_ID, pmId);
			mongo.coll.update(dbObject, new BasicDBObject("$set",
					new BasicDBObject(PM_ID, pmId)));
			// mongo.coll.apply(dbObject);
			if (i++ % 1000 == 0)
				System.err.println("updated " + pmId);
		}

	}
 
Example 11
Source File: MongoDB.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private List<Seq> keywordInSequence(String in_field, String keyword) {
  List<Seq> seqs = new ArrayList<Seq>();
  BasicDBObject query = new BasicDBObject();
  query.put(in_field, keyword);

  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;
}
 
Example 12
Source File: MongoDB.java    From act with GNU General Public License v3.0 5 votes vote down vote up
public List<Long> getRxnsWithSubstrate(String enzyme, Long org, List<Long> substrates) {
  BasicDBObject query = new BasicDBObject();
  query.put("organisms.id", org);
  BasicDBObject enzymeQuery = new BasicDBObject();
  enzymeQuery.put("ecnum", enzyme);
  query.put("$ne", enzymeQuery);
  for (Long substrate: substrates) {
    BasicDBList queryList = new BasicDBList();
    DBObject querySubstrate = new BasicDBObject();
    querySubstrate.put("enz_summary.substrates.pubchem", substrate);
    DBObject queryProduct = new BasicDBObject();
    queryProduct.put("enz_summary.products.pubchem", substrate);
    queryList.add(querySubstrate);
    queryList.add(queryProduct);
    query.put("$or", queryList);
  }

  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;
}
 
Example 13
Source File: MongoNativeExtractor.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates shard chunks.
 *
 * @param collection the collection
 * @return the deep partition [ ]
 */
private DeepPartition[] calculateShardChunks(DBCollection collection) {

    DBCursor chuncks = getChunks(collection);

    Map<String, String[]> shards = getShards(collection);

    MongoPartition[] deepPartitions = new MongoPartition[chuncks.count()];
    int i = 0;
    boolean keyAssigned = false;
    String key = null;
    while (chuncks.hasNext()) {

        DBObject dbObject = chuncks.next();
        if (!keyAssigned) {
            Set<String> keySet = ((DBObject) dbObject.get("min")).keySet();
            for (String s : keySet) {
                key = s;
                keyAssigned = true;
            }
        }
        deepPartitions[i] = new MongoPartition(mongoDeepJobConfig.getRddId(), i,
                new DeepTokenRange(shards.get(dbObject.get
                        ("shard")),
                        ((DBObject) dbObject.get
                                ("min")).get(key),
                        ((DBObject) dbObject.get("max")).get(key)), key);
        i++;
    }
    List<MongoPartition> mongoPartitions = Arrays.asList(deepPartitions);

    Collections.shuffle(mongoPartitions);
    return mongoPartitions.toArray(new MongoPartition[mongoPartitions.size()]);
}
 
Example 14
Source File: FanoutOnWriteTimeBuckets.java    From socialite with Apache License 2.0 5 votes vote down vote up
private List<Content> getContentFromBuckets(
        final User user,  final String contentField, final int limit) {
    
    List<Content> result = new ArrayList<Content>(limit);

    // Get a cursor for looking at buckets back in time
    DBCursor cursor = buckets.find(
        findBy(subField(BUCKET_ID_KEY, BUCKET_OWNER_KEY), user.getUserId()), 
        getFields(contentField)).
        sort(sortByDecending(BUCKET_ID_KEY)).batchSize(config.bucket_read_batch_size);
    
    try{
        // Go through buckets grabbing content until limit is
        // hit or we run out of buckets
        while(cursor.hasNext() && result.size() < limit){
            DBObject currentBucket = cursor.next();
            @SuppressWarnings("unchecked")
            List<DBObject> contentList = (List<DBObject>)currentBucket.get(contentField);
            int bucketSize = contentList == null ? 0 : 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;
}
 
Example 15
Source File: BuguFS.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
private List<GridFSDBFile> toFileList(DBCursor cursor){
    List<GridFSDBFile> list = new ArrayList<GridFSDBFile>();
    while(cursor.hasNext()){
        DBObject dbo = cursor.next();
        list.add((GridFSDBFile)dbo);
    }
    cursor.close();
    return list;
}
 
Example 16
Source File: MongoDB.java    From act with GNU General Public License v3.0 5 votes vote down vote up
public List<Long> getRxnsWithEnzyme(String enzyme, Long org, List<Long> substrates) {
  BasicDBObject query = new BasicDBObject();
  query.put("ecnum", enzyme);
  query.put("organisms.id", org);
  for (Long substrate : substrates) {
    BasicDBObject mainQuery = new BasicDBObject();
    mainQuery.put("$ne", substrate);
    BasicDBList queryList = new BasicDBList();
    BasicDBObject productQuery = new BasicDBObject();
    productQuery.put("enz_summary.products.pubchem", mainQuery);
    BasicDBObject substrateQuery = new BasicDBObject();
    substrateQuery.put("enz_summary.substrates.pubchem", mainQuery);
    queryList.add(substrateQuery);
    queryList.add(productQuery);
    query.put("$or", queryList);
  }
  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;
}
 
Example 17
Source File: MongoDB.java    From act with GNU General Public License v3.0 5 votes vote down vote up
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;
  }
 
Example 18
Source File: MapperUtil.java    From bugu-mongo with Apache License 2.0 5 votes vote down vote up
public static <T> List<T> toList(Class<T> clazz, DBCursor cursor, boolean withoutCascade){
    List<T> list = new ArrayList<>();
    try {
        while(cursor.hasNext()){
            DBObject dbo = cursor.next();
            list.add(fromDBObject(clazz, dbo, withoutCascade));
        }
    } finally {
        cursor.close();
    }
    return list;
}
 
Example 19
Source File: MongoQueryTest.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@Test
public void greaterThan() throws Exception {

    UUID appId = emf.lookupApplication( "test-organization/test-app" );
    EntityManager em = emf.getEntityManager( appId );

    Map<String, Object> properties = new LinkedHashMap<String, Object>();
    properties.put( "name", "Kings of Leon" );
    properties.put( "genre", "Southern Rock" );
    properties.put( "founded", 2000 );
    em.create( "greaterthan", properties );

    properties = new LinkedHashMap<String, Object>();
    properties.put( "name", "Stone Temple Pilots" );
    properties.put( "genre", "Rock" );
    properties.put( "founded", 1986 );
    em.create( "greaterthan", properties );

    properties = new LinkedHashMap<String, Object>();
    properties.put( "name", "Journey" );
    properties.put( "genre", "Classic Rock" );
    properties.put( "founded", 1973 );
    em.create( "greaterthan", properties );

    // See http://www.mongodb.org/display/DOCS/Java+Tutorial

    Mongo m = new Mongo( "localhost", 27017 );

    DB db = m.getDB( "test-organization/test-app" );
    db.authenticate( "test", "test".toCharArray() );

    BasicDBObject query = new BasicDBObject();
    query.put( "founded", new BasicDBObject( "$gt", 1973 ) );

    DBCollection coll = db.getCollection( "greaterthans" );
    DBCursor cur = coll.find( query );

    assertTrue( cur.hasNext() );

    DBObject result = cur.next();
    assertEquals( "Stone Temple Pilots", result.get( "name" ) );
    assertEquals( "Rock", result.get( "genre" ) );

    result = cur.next();
    assertEquals( "Kings of Leon", result.get( "name" ) );
    assertEquals( "Southern Rock", result.get( "genre" ) );

    assertFalse( cur.hasNext() );
}
 
Example 20
Source File: ProjectListResource.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public Representation doRepresent() {
      // Ready query params
      String _page = getQueryValue("page");
      String _size = getQueryValue("size");

ProjectRepository projectRepo = platform.getProjectRepositoryManager().getProjectRepository();
       
      DBCursor cursor;
      if(_page != null && !"".equals(_page) && isInteger(_page) && _size != null && !"".equals(_size) && isInteger(_size)) {
      	int page = Integer.valueOf(_page);
      	int pageSize = Integer.valueOf(_size);
      	cursor = projectRepo.getProjects().getDbCollection().find().skip(page*pageSize).limit(pageSize);
      } else {
      	cursor = projectRepo.getProjects().getDbCollection().find();
      }
      
      ArrayNode projects = mapper.createArrayNode();
      
      while (cursor.hasNext()) {
          try {
          	DBObject p = cursor.next();
          	p.removeField("storage");
		p.removeField("metricProviderData");
		p.removeField("_superTypes");
		p.removeField("_id");
		
		// FIXME: Temporary solution
		p.removeField("licenses");
		p.removeField("persons");
		
		projects.add(mapper.readTree(p.toString()));
          } catch (Exception e) {
          	System.err.println("Error: " + e.getMessage());
		ObjectNode m = mapper.createObjectNode();
		m.put("apicall", "list-all-projects");
		return Util.generateErrorMessageRepresentation(m, e.getMessage());
          }
      }
      
      cursor.close();
      
      StringRepresentation resp = new StringRepresentation(projects.toString());
resp.setMediaType(MediaType.APPLICATION_JSON);
return resp;
  }