[英]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.