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

Aggregation:com.mongodb.MongoCommandException: Command failed with error 16436:

我正在使用 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 但没有用。

提前致谢。

这是下面 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"
        }
    }]
}