繁体   English   中英

我的聚合和查找代码是否有我遗漏的东西?

[英]Is there something I am missing with my aggregate and find code?

所以我有一个用于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;
    }

}

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

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

所以我发布了自己的答案,因为显然代码工作正常。 我的输入是错误的,我学习如何为MongoDB设置它,我设置了寻找输入,通常json的格式是{“key”:“value”},所以当我输入时价值部分,我希望它在引号中。 但显然如果你不把它放在引号中,它的效果就像预期的那样完美。 我在Python中工作得很好所以我有一个工作的例子来看看。 此外,项目部分中的那些1在我将它们从字符串格式中取回整数格式之后才会起作用,这些1的真正意思是“真实”,因为它只是投影那些键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM