gpt4 book ai didi

java - Mongodb 未知错误?

转载 作者:行者123 更新时间:2023-11-29 06:17:58 25 4
gpt4 key购买 nike

我是 Mongo DB 的新手,我必须用 Java 实现它。我浏览了可能的幻灯片,但我对发生的事情感到困惑。我使用 mongo DB 执行了一个小的 java 程序,但它不工作?

我的java代码:

    public class MongoDbTesting {

public void connectingMongo() throws UnknownHostException, MongoException{
Mongo m = new Mongo("localhost" , 27017); //mongo object
DB db = m.getDB("todo");
System.out.println("Connected");
//making a collection object which is table when compared to sql
DBCollection items = db.getCollection("items");
System.out.println("items got");

//to work with document we need basicDbObject
BasicDBObject query = new BasicDBObject();
System.out.println("Created mongoObject");
//insert in mongo
query.put("priority", "highest");
items.insert(query);
System.out.println("Inserted");
//Cursor, which is like rs in sql
DBCursor cursor = items.find();
System.out.println("items got");
//print highest priority items

while(cursor.hasNext()){
System.out.println(cursor.hasNext());
}
}
}

输出是: 它被连续打印为

是的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的真的是的

我不知道发生了什么。我想将一些数据插入到集合“项目”中另外,如果我想知道如何在 Java 中使用 Mongo。我很了解 mysql 但转向 mongo 我无法在查询中将两者联系起来。 “query.put”在做什么?有什么建议吗?

最佳答案

因为您忘记在 while 循环中调用 cursor.next(),所以您陷入了无限循环。

关于java - Mongodb 未知错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313222/

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