mongodb java group,如何在Java中使用mongodb $ group?

I have a collection processedClickLog in MongoDB.

{

"_id" : ObjectId("58ffb4cefbe21fa7896e2d73"),

"ID" : "81a5d7f48e5df09c9bc006e7cc89d6e6",

"USERID" : "206337611536",

"DATETIME" : "Fri Mar 31 17:29:34 -0400 2017",

"QUERYTEXT" : "Tom",

"DOCID" : "www.demo.com",

"TITLE" : "Harry Potter",

"TAB" : "People-Tab",

"TOTALRESULTS" : "1",

"DOCRANK" : 1

}

{ "id":

....

}

I am trying to execute a complex query in java. My query is to get processedClickLog collection where

TAB is not equal to People-Tab

DOCRANK is not equal to 0

only return "USERID", "DOCID", "DOCRANK", "QUERYTEXT" fields

Group by USERID

Below is my Java code. I am able to satisfy the first three condition. But I am stuck on 4th condition which is group by USERID.

String jsonResult = "";

MongoClient mongoClient = new MongoClient("localhost", 27017);

MongoDatabase database = mongoClient.getDatabase("test1");

MongoCollection collection = database.getCollection("processedClickLog");

//add condition where TAB is not equal to "People-Tab" and DOCRANK is not equal to 0

List criteria = new ArrayList();

criteria.add(new BasicDBObject("DOCRANK", new BasicDBObject("$ne", 0)));

criteria.add(new BasicDBObject("TAB", new BasicDBObject("$ne", "People-Tab")));

//combine the above two conditions

BasicDBObject query = new BasicDBObject("$and", criteria);

//to retrieve all the documents with specific fields

MongoCursor cursor = collection.find(query)

.projection(Projections.include("USERID", "DOCID", "DOCRANK", "QUERYTEXT")).iterator();

try {

while (cursor.hasNext()) {

System.out.println(cursor.next().toJson());

}

} finally {

cursor.close();

}

System.out.println(hashMap);

mongoClient.close();

}

How should I define my whole query to add the condition "group by USERID" in java? Any help is appreciated

解决方案

You've to use aggregation framework. Statically import all the methods of helper classes and use the below code.

Use of BasicDBObject is not required in newer 3.x driver api. You should use the new class Document for similar needs.

import static com.mongodb.client.model.Accumulators.*;

import static com.mongodb.client.model.Aggregates.*;

import static java.util.Arrays.asList;

import static com.mongodb.client.model.Filters.*;

import static com.mongodb.client.model.Projections.*;

Bson match = match(and(ne("DOCRANK", 0), ne("TAB", "People-Tab")));

Bson group = group("$USERID", first("USERID", "$USERID"), first("DOCID", "$DOCID"), first("DOCRANK", "$DOCRANK"), first("QUERYTEXT", "$QUERYTEXT"));

Bson projection = project(fields(include("USERID", "DOCID", "DOCRANK", "QUERYTEXT"), excludeId()));

MongoCursor cursor = collection.aggregate(asList(match, group, projection)).iterator();

Projection stage is optional, only added to give a complete example.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值