聚合:com.mongodb.MongoCommandException:命令失败,错误16436:

时间:2016-09-13 06:42:35

标签: mongodb aggregation-framework mongo-java-driver

我正在使用mongodb并希望将我的日志数据存储在文档中的表单数组中。在阅读收藏品时 我正在使用聚合管线。当我厌倦了在Mongo Booster中使用查询时,查询工作正常,但它正在给予 我试图通过Java程序使用它时出现以下异常。

详细说明:    - > db.version() - 3.2.7    - > mongo-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但没有用。

提前致谢。

1 个答案:

答案 0 :(得分: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"
        }
    }]
}