gpt4 book ai didi

java - 我的 MongoDB 日志系统的行为与预期不同。为什么?

转载 作者:行者123 更新时间:2023-12-01 10:41:23 25 4
gpt4 key购买 nike

我正在开发一个 MongoDB 日志系统,但它的行为并不符合我的预期。我不知道程序的结构是否正确。
我想在数据库 (MongoDB) 中搜索并打印所有事件,但是当我有 pageLoad 事件时,我想检查它是否有 URL 并打印它,否则它应该再次搜索下一个事件并尝试相同的行为,作为循环。

我期望的结果必须是这样的,例如:

  • 鼠标移动
  • 鼠标移动
  • 鼠标移动
  • 点击
  • 滚动
  • 点击
  • 页面加载....//htttp://www......url(1).....e.x
  • 鼠标移动
  • 点击
  • 页面加载....//htttp://www......url(2).....e.x
<小时/>

这是代码:

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);

while (cursorEvents.hasNext()) {
DBObject documentInEventCollection = cursorEvents.next();

System.out.println(cursorEvents.next().get("type").toString());

if ("pageLoad".equals(documentInEventCollection.get("type"))) {

System.out.println(cursorEvents.next().get("url").toString());
} else {

System.out.println(cursorEvents.next().get("type").toString());
}
}
mongoClient.close();
<小时/>

但是当我运行程序时,得到的结果是这样的,例如:

  • 窗口大小
  • 鼠标移动
  • 点击向上
  • 点击

...就到此为止了。

<小时/>

出了什么问题?

最佳答案

您在循环中调用了 cursorEvents.next() 三次,而不是一次。每次调用 next() 时,都会转到...下一个元素。

使用您的 documentInEventCollection 变量:

while (cursorEvents.hasNext()) {
DBObject documentInEventCollection = cursorEvents.next();

System.out.println(documentInEventCollection.get("type").toString());

if ("pageLoad".equals(documentInEventCollection.get("type"))) {

System.out.println(documentInEventCollection.get("url").toString());
} else {

System.out.println(documentInEventCollection.get("type").toString());
}

}

关于java - 我的 MongoDB 日志系统的行为与预期不同。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34392071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com