2016-09-13 294 views
0

我正在使用mongodb並希望將我的日誌數據存儲在文檔中的表單數組中。從集合 閱讀時,我正在使用聚合管道。當我厭倦了在Mongo Booster中使用查詢時,查詢工作正常,但當我試圖通過Java程序使用它時,它給出了以下例外 。聚集:com.mongodb.MongoCommandException:命令失敗,錯誤16436:

詳情: - > db.version() - 3.2.7 - >蒙戈 - java_driver:3.2.2

Query in Mongo Booster: 
======================= 
db.logCollection.aggregate({$unwind:'$logList'},{ $sort : {'logList.log.timestamp': -1} },{ $match:{'logList.log.userId': "100100"}},{ $group: {_id: null, logList: {$push: '$logList'}}},{ $project: { _id: 0,logList: {log:{timestamp: 1,operation:1}}}}).pretty() 

Query: using Java 
================= 

DBObject unwindField = new BasicDBObject("$unwind", "$logList"); 
    DBObject groupFields = new BasicDBObject("_id", null);   
    groupFields.put("logList", new BasicDBObject("$push","$logList")); 
    DBObject group = new BasicDBObject("$group", groupFields); 
    DB logDB = mongoClient.getDB("logdb"); 
    DBCollection collection=logDB.getCollection(collectionName);  
    DBObject skipFields = new BasicDBObject("$skip",skip); 
    DBObject limitFields = new BasicDBObject("$limit",limit); 
    Iterable<DBObject> results =null; 
    try { 
    results= collection.aggregate(unwindField, sortField,searchField,skipFields,limitFields,group,projectFields).results(); 
    } catch (Exception e) { 
    log.error("readLogsFromCollection() Failed"); 

    } 

Exception: 
========== 
com.mongodb.MongoCommandException: Command failed with error 16436: 'Unrecognized pipeline stage name: 'logList.log.timestamp' on server localhost:27017. 
The full response is { "ok" : 0.0, "errmsg" : "Unrecognized pipeline stage name: 'logList.log.timestamp'", "code" : 16436 } 

Input Document: 
================ 

{ 
    "logList" : [ 
     { 
      "log" : { 
       "acctId" : "0", 
       "info1" : { 
        "itemName" : "-", 
        "value" : "-" 
       }, 
       "errorCode" : "", 
       "internalInformation" : "", 
       "kind" : "Infomation", 
       "groupId" : "0", 
       "logId" : "G1_1", 
       "operation" : "startDiscovery", 
       "result" : "normal", 
       "userId" : "100100", 
       "timestamp" : "1470980265729" 
      } 
     } 
    ] 
} 

可以在任何身體告訴我,可能是什麼問題,我讀問題與版本有關,但我用mongo-java_driver-3.3也沒用。

在此先感謝。

+0

能否請您補充一點,在工作查詢米你想在java中編碼的ongodb shell?另外,如果您可以添加示例文檔,它將很有用。 – notionquest

+0

你好@notionquest,請查看更新後的查詢 –

+0

請提供樣品文件? – notionquest

回答

1

這是下面的MongoDB查詢的Java代碼。我已經使用了與OP中提到的相同的Java驅動程序(mongo-java_driver:3.2.2)。

MongoDB的查詢: -

db.loglist.aggregate({$unwind:'$logList'}, 
{ $sort : {'logList.log.timestamp': -1} }, 
{ $match:{'logList.log.userId': "100100"}}, 
{ $group: {_id: null, logList: {$push: '$logList'}}}, 
{ $project: { _id: 0,logList: {log:{timestamp: 1,operation:1}}}}).pretty(); 

Java代碼: -

public static void main(String[] args) { 
     MongoClient client = new MongoClient(); 
     MongoDatabase database = client.getDatabase("test"); 

     AggregateIterable<Document> mongoCollectionList = database.getCollection("loglist") 
       .aggregate(Arrays.asList(Aggregates.unwind("$logList"), Aggregates.sort(Sorts.descending("logList.log.timestamp")), 
         Aggregates.match(Filters.eq("logList.log.userId", "100100")), 
         Aggregates.group("$id", Accumulators.push("logList", "$logList")), 
         Aggregates.project(Projections.include("logList.log.timestamp", "logList.log.operation")) 
         )); 

     MongoCursor<Document> mongoCursor = mongoCollectionList.iterator(); 

     while (mongoCursor.hasNext()) { 
      System.out.println(mongoCursor.next().toJson()); 

     } 

    } 

輸出: -

{ 
    "_id": null, 
    "logList": [{ 
     "log": { 
      "operation": "startDiscovery", 
      "timestamp": "1470980265729" 
     } 
    }] 
} 
+0

Hello @notionnquest,我正在使用depricated API。我們可以使它與我現在使用的舊API一起工作 –

+0

使用棄用的API的原因是什麼? – notionquest

+0

它在代碼的所有地方都存在,現在我應該繼續使用它。它工作@notionquest,因爲匹配和排序沒有聚合管道運算符錯過了我的Java代碼,它沒有工作。現在它的工作。 –