我正在使用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也沒用。
在此先感謝。
能否請您補充一點,在工作查詢米你想在java中編碼的ongodb shell?另外,如果您可以添加示例文檔,它將很有用。 – notionquest
你好@notionquest,請查看更新後的查詢 –
請提供樣品文件? – notionquest