我的汇总和查找代码中缺少什么吗?

时间:2019-03-30 22:23:27

标签: java mongodb find aggregate

所以我有一个适用于mongoDB的有效Python代码,对于我现在正在上的一类,我正尝试将此代码放入Java中。目前,除了聚合部分外,其他所有工作都正常进行,还有一个find命令应该产生多个结果。

我进行了一些研究,结果表明它必须处于循环状态才能正确打印,但是我遇到了同样的错误。我还发现了一些有关它可能是超时的游标的信息,这是我正在使用的一个很大的文件,但某些结果却无法正常工作,例如.noCursorTimeout(true)。

这是我为汇总设置的设置:

public class Aggregate {

    public static Object AggregateDocument(String input) {

        //Get mongo set up
        @SuppressWarnings("resource")
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("market");
        MongoCollection<Document> collection = database.getCollection("stocks");

        //Get the documents set up for each section of the aggregate command
        //Set up the Match
        Document match = new Document("$match", new Document("Sector", input));
        //Set up the Project
        Document project1 = new Document("_id", 1);
        Document project2 = project1.append("Shares Outstanding", 1);
        Document project3 = project2.append("Industry", 1);
        Document project4 = project3.append("Sector", 1);
        Document project = new Document("$project", project4);
        //Set up the Group
        Document group1 = new Document("_id", "$Industry");
        Document group2 = group1.append("Total Outstanding Shares", new Document("$sum", "$Shares Outstanding"));
        Document group = new Document("$group", group2);

        //Call the aggregate command
        AggregateIterable<Document> agg = collection.aggregate(Arrays.asList(match, project, group));

        //Print out the result
        for (Document printAggs : agg)
        {
            System.out.println(printAggs);
        }

        return agg;
    }
}

这是我的“查找”命令的代码

public class FindStr {

    public static Object FindString(String input) {

        //Get mongo set up
        @SuppressWarnings("resource")
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("market");
        MongoCollection<Document> collection = database.getCollection("stocks");

        //Get the documents set up 
        Document doc1 = new Document("Industry", input);
        Document doc2 = new Document("Ticker", 1);

        //Set up the find() command
        MongoCursor<Document> cursor = collection.find(doc1).projection(doc2).noCursorTimeout(true).iterator();

        //Print out the results
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }


        return cursor;
    }

}

对于聚合命令,我应该获取一个适合该聚合的文档列表,对于查找字符串命令,该列表几乎相同。我得到的结果彼此相似,例如,这是我从查找字符串输出中得到的结果:

Enter in a string to search for (enter it in quotes), or b to go back: 
"Medical Laboratories & Research"
Mar 30, 2019 5:52:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Mar 30, 2019 5:52:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Mar 30, 2019 5:52:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:3, serverValue:74}] to localhost:27017
Mar 30, 2019 5:52:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 6]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, roundTripTimeNanos=477995}
Mar 30, 2019 5:52:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:4, serverValue:75}] to localhost:27017

1 个答案:

答案 0 :(得分:0)

所以我发布自己的答案,因为显然代码可以正常工作。我的输入是错误的,我学会了如何为MongoDB设置它,我让它设置为寻找输入,通常json的格式为{“ key”:“ value”},所以当我估算有价值的部分,我希望它能用引号引起来。但是很显然,如果您不将其用引号引起来,它可以按预期运行。我在Python中可以正常工作,因此我有一个工作示例可供查看。另外,在项目部分中的那些1直到我将它们从字符串格式中取出并转换为整数格式后才起作用,这些1的真正含义是“ true”,因为它只能投影那些键。