// Requires official Java MongoDB Driver 3.6+
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Arrays;
import java.util.List;
import org.bson.BsonNull;
import org.bson.Document;
import org.bson.conversions.Bson;
public class Program {
public static void main(String[] args) {
try (MongoClient client = new MongoClient("localhost", 27017)) {
MongoDatabase database = client.getDatabase("bijiduo");
MongoCollection<Document> collection = database.getCollection("docInfo");
// Created with Studio 3T, the IDE for MongoDB -
https://studio3t.com/ Block<Document> processBlock = new Block<Document>() {
@
Override public void apply(final Document document) {
System.out.println(document);
}
};
List<? extends Bson> pipeline = Arrays.asList(
new Document()
.append("$match", new Document()
.append("sessionId", new Document()
.append("$in", Arrays.asList(
"1249264194773536859"
)
)
)
.append("channel", 3.0)
.append("senderType", 2.0)
),
new Document()
.append("$group", new Document()
.append("_id", new Document()
.append("sessionId", "$sessionId")
.append("targetId", "$targetId")
)
.append("count", new Document()
.append("$sum", 1.0)
)
),
new Document()
.append("$match", new Document()
.append("tcount", new Document()
.append("$gt", 2.0)
)
),
new Document()
.append("$group", new Document()
.append("_id", new Document()
.append("targetId", "$_id.targetId")
)
.append("count", new Document()
.append("$sum", 1.0)
)
),
new Document()
.append("$group", new Document()
.append("_id", new BsonNull())
.append("count", new Document()
.append("$sum", 1.0)
)
)
);
collection.aggregate(pipeline)
.allowDiskUse(false)
.forEach(processBlock);
} catch (MongoException e) {
// handle MongoDB exception
}
}
}