Package com.mongodb

Examples of com.mongodb.DBCursor


        //collection.drop();
        return collection;
    }

    private static void printCollection(final DBCollection collection) {
        DBCursor cursor = collection.find().sort(new BasicDBObject("_id", 1));
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }

    }
View Full Code Here


        DB db = client.getDB("course");
        db.setReadPreference(ReadPreference.primary());
        DBCollection coll = db.getCollection("write.test");
        coll.setReadPreference(ReadPreference.primaryPreferred());

        DBCursor cursor = coll.find().setReadPreference(ReadPreference.nearest());
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }
        } finally {
            cursor.close();
        }
    }
View Full Code Here

    public static void main(String[] args) throws UnknownHostException {
        MongoClient mongoClient = new MongoClient();
        DB school = mongoClient.getDB("school");
        DBCollection studentsCollection = school.getCollection("students");

        DBCursor students = studentsCollection.find();

        DBObject student = null;
        while(students.hasNext()) {
            student = students.next();

            List<DBObject> scores = (List<DBObject>)student.get("scores");

            DBObject lowestScore = null;
View Full Code Here

        return post;
    }

    public List<DBObject> findByDateDescending(int limit) {
        List<DBObject> posts;
        DBCursor cursor = postsCollection.find().sort(new BasicDBObject().append("date", -1)).limit(limit);
        try {
            posts = cursor.toArray();
        } finally {
            cursor.close();
        }
        return posts;
    }
View Full Code Here

    public List<DBObject> findByTagDateDescending(final String tag) {
        List<DBObject> posts;
        BasicDBObject query = new BasicDBObject("tags", tag);
        System.out.println("/tag query: " + query.toString());
        DBCursor cursor = postsCollection.find(query).sort(new BasicDBObject().append("date", -1)).limit(10);
        try {
            posts = cursor.toArray();
        } finally {
            cursor.close();
        }
        return posts;
    }
View Full Code Here

        return post;
    }

    public List<DBObject> findByDateDescending(int limit) {
        List<DBObject> posts;
        DBCursor cursor = postsCollection.find().sort(new BasicDBObject().append("date", -1)).limit(limit);
        try {
            posts = cursor.toArray();
        } finally {
            cursor.close();
        }
        return posts;
    }
View Full Code Here

    public List<DBObject> findByTagDateDescending(final String tag) {
        List<DBObject> posts;
        BasicDBObject query = new BasicDBObject("tags", tag);
        System.out.println("/tag query: " + query.toString());
        DBCursor cursor = postsCollection.find(query).sort(new BasicDBObject().append("date", -1)).limit(10);
        try {
            posts = cursor.toArray();
        } finally {
            cursor.close();
        }
        return posts;
    }
View Full Code Here

  }

  @Override
  public List<Message> getLastMessages() {
    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");
    DBCursor curs = messagesCollection.find();

    List<Message> messages = new ArrayList<>();
    while (curs.hasNext()) {
      DBObject o = curs.next();
      String test = o.get("message").toString();
      Gson gson = new Gson();
      Message message = gson.fromJson(test, Message.class);
      messages.add(message);
    }
View Full Code Here

  public void onlyTenMessages() {
    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");
    long nbMessages = messagesCollection.count();
    while (nbMessages > 10) {
      // enregistrement le plus vieux
      DBCursor oldestMessage = messagesCollection.find().sort(new BasicDBObject("date", 1)).limit(1);
      messagesCollection.remove(oldestMessage.next());
      nbMessages = messagesCollection.count();
    }

  }
View Full Code Here

    MessagesHistoryMongo conect = new MessagesHistoryMongo();

    DBCollection messagesCollection = chatDatabase.getCollection("historymessages");
   
    Long numberOfMessages = messagesCollection.count();
    DBCursor curs = messagesCollection.find();
    List<Message> messages = new ArrayList<>();
    while (curs.hasNext()) {
      DBObject o = curs.next();
      String test=o.get("message").toString();
      Gson gson = new Gson();
      Message message = gson.fromJson(test, Message.class);     
      messages.add(message);
    }
 
    Assert.assertEquals(new Long(10), numberOfMessages);
    Assert.assertEquals(new Date(1394743482).toString(),messages.get(0).getDate());
   
    Message message11 = new Message();
    message11.setMessage("un best OF!");
    message11.setUserFrom(new User("antho", Statut.EN_LIGNE, "192.168.1.3"));
    message11.setUserTo("all");
    message11.setEvent(Event.ALL_CLIENTS);
    message11.setDate(new Date(1395621082).toString());

    conect.saveMessage(message11);
    Long numberOfMessagesNew = messagesCollection.count();
    Assert.assertEquals(new Long(10), numberOfMessagesNew);
   
    DBCursor cursNew = messagesCollection.find();
    List<Message> messagesNew = new ArrayList<>();
    while (cursNew.hasNext()) {
      DBObject o = cursNew.next();
      String objectMessageNew=o.get("message").toString();
      Gson gson = new Gson();
      Message messageNew = gson.fromJson(objectMessageNew, Message.class);     
      messagesNew.add(messageNew);
    }
View Full Code Here

TOP

Related Classes of com.mongodb.DBCursor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.