我有以下代码将相关文档获取到给定的输入,输入是categoryIds,personIds和type,对于类别,它返回预期的结果并正确地对其进行排序,它获取与输入匹配的所有类别,并且将他们排序到最相关的顶部,现在我想添加人员并将最相关的结果也放置在顶部,但是我不知道如何将其与类别分组。
这是我的代码:
Criteria exist = Criteria.where("personIds._id").exists(false);
Criteria pe = Criteria.where("personIds._id").in(Arrays.asList("2411"));
Criteria persons = new Criteria();
persons.orOperator(pe, exist);
Document cat = new Document("categoryIds._id",
new Document("$in", Arrays.asList("7908", "7892", "7883", "7860")));
Document type = new Document("type", "PROGRAM");
Document categoryMatch = new Document("$match", cat);
Document categoryUnwind = new Document("$unwind", "$categoryIds");
Document match = new Document("$match", cat);
Document personMatch = new Document("$match", persons.getCriteriaObject());
AggregateIterable<Document> output = collection.aggregate(Arrays.asList(
categoryMatch, categoryUnwind, match,
personMatch,
new Document("$match", type),
new Document("$group", new Document("_id", "$" + "_id._id").append("count", new Document("$sum", 1))),
new Document("$sort", new Document("count", -1))
));
for (Document dbObject : output) {
System.out.println("---> " + dbObject.toJson());
}
此代码的结果:
它将查找具有给定类别ID的所有类别,并根据在同一文档中找到的匹配项对它们进行排序。
这是输出:
{ "_id" : "48999", "count" : 4 } --> this product match all the catgories
{ "_id" : "141523", "count" : 3 } --> this product has 3 matches
{ "_id" : "149366", "count" : 3 }
{ "_id" : "96084", "count" : 3 }
现在,当类别相等时,我想按personId排序,请问有人可以澄清