使用 mongodb 和 java
Using mongodb with java
我正在数据库中搜索 URL,但使用此代码我做不到。为什么?通常我想打印此数据库中存在的所有类型和 URL 。当我打印时只有类型有效但打印为 URL 什么都没有。
MongoClient mongoClient;
DB db;
mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDB("behaviourDB_areas");
DBCollection cEvent = db.getCollection("event");
BasicDBObject orderBy = new BasicDBObject();
orderBy.put("timeStamp",1);
DBCursor cursorEvents = null;
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("user_id", "55b20db905f333defea9827f");
cursorEvents = cEvent.find(searchQuery).sort(orderBy);
int count=0;
if(cursorEvents.hasNext()){
while(cursorEvents.hasNext()){
count++;
System.out.println(cursorEvents.next().get("type").toString());
System.out.println(cursorEvents.next().get("url").toString());
System.out.println(count);
}
}
mongoClient.close();
}
}
cursor.next()
应该只调用一次,调用它第二次将 return 下一个文档。 documentation
NullPointerException
可能会被抛出,因为下一个文档不存在或 get("url")
returns null
.
以下代码段应该可以解决这两个问题。
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase db = mongoClient.getDatabase("behaviourDB_areas");
MongoCollection cEvent = db.getCollection("event", Document.class);
MongoCursor<Document> cursorEvents = cEvent
.find(new BasicDBObject("user_id", "55b20db905f333defea9827f"))
.sort(new BasicDBObject("timeStamp",1))
.iterator();
int count = 0;
if(cursorEvents.hasNext()) {
Document doc = cursorEvents.next();
System.out.println(doc.getString("type"));
if (doc.containsKey("url")) {
System.out.println(doc.getString("url"));
}
System.out.println(++count);
}
cursorEvents.close();
mongoClient.close();
我正在数据库中搜索 URL,但使用此代码我做不到。为什么?通常我想打印此数据库中存在的所有类型和 URL 。当我打印时只有类型有效但打印为 URL 什么都没有。
MongoClient mongoClient;
DB db;
mongoClient = new MongoClient("localhost", 27017);
db = mongoClient.getDB("behaviourDB_areas");
DBCollection cEvent = db.getCollection("event");
BasicDBObject orderBy = new BasicDBObject();
orderBy.put("timeStamp",1);
DBCursor cursorEvents = null;
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("user_id", "55b20db905f333defea9827f");
cursorEvents = cEvent.find(searchQuery).sort(orderBy);
int count=0;
if(cursorEvents.hasNext()){
while(cursorEvents.hasNext()){
count++;
System.out.println(cursorEvents.next().get("type").toString());
System.out.println(cursorEvents.next().get("url").toString());
System.out.println(count);
}
}
mongoClient.close();
}
}
cursor.next()
应该只调用一次,调用它第二次将 return 下一个文档。 documentation
NullPointerException
可能会被抛出,因为下一个文档不存在或 get("url")
returns null
.
以下代码段应该可以解决这两个问题。
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase db = mongoClient.getDatabase("behaviourDB_areas");
MongoCollection cEvent = db.getCollection("event", Document.class);
MongoCursor<Document> cursorEvents = cEvent
.find(new BasicDBObject("user_id", "55b20db905f333defea9827f"))
.sort(new BasicDBObject("timeStamp",1))
.iterator();
int count = 0;
if(cursorEvents.hasNext()) {
Document doc = cursorEvents.next();
System.out.println(doc.getString("type"));
if (doc.containsKey("url")) {
System.out.println(doc.getString("url"));
}
System.out.println(++count);
}
cursorEvents.close();
mongoClient.close();