I am using mongodb and wanted to store my log data in the form array in the document. While reading from the collection I am using aggregation pipe line. When I tired to use the query in Mongo Booster, the Query is working fine but it was giving the following exception when I tried to use it by Java Program.
Details: --> 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"
}
}
]
}
Could any body tell me what might be the problem, I read the issue is with version, but I Used mongo-java_driver-3.3 also but no use.
Thanks in advance.