我有以下文件(@Document):
@Id
private String id;
private String fileName;
private String projectId;
private List<DocumentFileVersion> documentFileVersions;
private List<String> userIdBlackList; // here userIds are included
这是我目前的聚合:
final String userId = "5589929b887dc1fdb501cdbb";
final Aggregation aggregate = newAggregation(match(Criteria.where("projectId").in(projectId)),
group("fileName").count().as("amountOfDocumentFiles"));
final AggregationResults<DocumentFileAmount> groupDocumentFiles = mongoTemplate.aggregate(aggregate, DocumentFile.class,
DocumentFileAmount.class);
final List<DocumentFileAmount> documentFileAmounts = groupDocumentFiles.getMappedResults();
final int amountOfDocumentFiles = documentFileAmounts.size();
现在,我将以这种方式扩展聚合,即只有在 userId(在本例中为“1234”)不在userIdBlackList中的 DocumentFiles 中。有没有可能这样做,比如在伪代码中:
final Aggregation aggregate = newAggregation(match(Criteria.where("projectId").in(projectId).and(userId).notInList("userIdBlackList")),
group("fileName").count().as("amountOfDocumentFiles"));
我需要这样的东西:... .and(userId).notInList("userIdBlackList") ...
[编辑] 我试过这个查询:
final Aggregation aggregate = newAggregation(
match(Criteria.where("projectId").in(projectId).and(userId).and("userIdBlackList").ne(userId)),
group("fileName").count().as("amountOfDocumentFiles"));
数据库条目可能如下所示:
{
"_id" : ObjectId("587e7cabafdaff28743f3034"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "Hydrangeas.jpg",
"projectId" : "587e7c95afdaff28743f302e",
"userIdBlackList" : [
"5589929b887dc1fdb501cdbb"
]
}
但是.and(userId).and("userIdBlackList").ne(userId)没有效果。
[编辑2]
我也尝试在 mongo 控制台中模拟它。我已经使用命令db.DocumentFile.find().pretty()列出了所有文档文件:
db.DocumentFile.find().pretty()
{
"_id" : ObjectId("587f0d61473c92b933a68efa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f0d61473c92b933a68ef9",
"active" : true,
"userIdBlackList" : [
"587f0d61473c92b933a68ef8"
]}
我的查询如下所示:
db.DocumentFile.aggregate({ "$match" : { "projectId" : { "$in" : [ "587f0d61473c92b933a68ef9"]} , "userIdBlackList" : { "$ne" : "587f0d61473c92b933a68ef8"}}}).pretty();
{
"_id" : ObjectId("587f0d61473c92b933a68efa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f0d61473c92b933a68ef9",
"active" : true,
"userIdBlackList" : [
"587f0d61473c92b933a68ef8"
]}
我原以为我没有得到文档文件,因为这个表达式"userIdBlackList" : { "$ne" : "587f0d61473c92b933a68ef8"} 有人知道我做错了什么吗?
[编辑3]
我有这两个文件和aggegate:
final Aggregation aggregate = newAggregation(
match(Criteria.where("projectId").in(projectId).and("userIdBlackList").nin(userId)),
group("fileName").count().as("amountOfDocumentFiles"));
我得到了 2 的数量,但应该是 1。我不知道我做错了什么?
db.DocumentFile.find().pretty()
{
"_id" : ObjectId("587f2228e232342f74b166f9"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile1",
"ending" : "jpg",
"projectId" : "587f2228e232342f74b166f8",
"active" : true,
"userIdBlackList" : [
"587f2228e232342f74b166f7"
]}
{
"_id" : ObjectId("587f2228e232342f74b166fa"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "DocumentFile2",
"ending" : "jpg",
"projectId" : "587f2228e232342f74b166f8",
"active" : true,
"userIdBlackList" : [ ]
}