1.问题 2.解决 db.text.aggregate( // Initial document match (uses index, if a suitable one is available) [ { $match: { _id : ObjectId("5ca95b4bfb60ec43b5dd0db5") }}, // Expand the scores array into a stream of documents { $unwind: '$tweet_list' }, { $match: { 'tweet_list.timestamp_ms': '1451841845660' }}, // Sort in descending order { $sort: { 'tweet_list.timestamp_ms': 1 }} ] ) java实现此聚合函数 Aggregation agg = Aggregation.newAggregation( Aggregation.match(Criteria.where("_id").is(id)), Aggregation.unwind("tweet_list"), Aggregation.sort(Sort.Direction.ASC,"tweet_list.timestamp_ms"), Aggregation.project("tweet_list.timestamp_ms","tweet_list.text","tweet_list.created_at") ); AggregationResults results = mongoTemplate.aggregate(agg, "text", JSONObject.class); //System.out.println("results"+results.getRawResults()); //获取到的结果是document //String res = results.getRawResults(); String json = com.mongodb.util.JSON.serialize(results.getRawResults()); System.out.println("JSON serialized Document: " + json); JSONObject jso= JSON.parseObject(json); JSONArray resultss=jso.getJSONArray("results"); System.out.println(resultss); 3.扩展 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自学php网的支持。
查看更多关于mongoDB中聚合函数java处理示例详解的详细内容...