2016-09-13 6 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

당신이 작동 쿼리를 추가시겠습니까 엠 자바에서 코딩하려고하는 ongodb 쉘? 또한 샘플 문서를 추가 할 수 있으면 유용합니다. – notionquest

+0

안녕하세요. @notionquest, 업데이트 된 질문을보세요 –

+0

샘플 문서를 보내주세요. – notionquest

답변

1

아래의 MongoDB 쿼리에 대한 Java 코드입니다. 나는 OP에서 언급 한 것과 같은 자바 드라이버 (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(); 

자바 코드 : -

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

안녕하세요 @notionnquest, depricated API를 사용하고 있습니다. 우리가 지금 사용하고있는 이전 API로 작업 할 수 있도록 할 수 있습니까? –

+0

비추천 API를 사용하는 이유는 무엇입니까? – notionquest

+0

코드의 모든 장소에 있었으므로 이제는 계속해야합니다. @notionquest가 작동합니다. 일치 및 정렬을 위해 집계 파이프 라인 연산자가 내 Java 코드에서 누락되었으므로 작동하지 않습니다. 이제 그 일. –

관련 문제