0

У меня есть следующий документ (@Document):Spring данные и MongoDB - агрегация со списком Java

@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(); 

Теперь я продлю aggreagation в так что у меня будет только DocumentFiles, где userId (в данном случае «1234») не находится в userIdBlackList. Есть ли возможность сделать это, как в псевдокоде:

final Aggregation aggregate = newAggregation(match(Criteria.where("projectId").in(projectId).and(userId).notInList("userIdBlackList")), 
    group("fileName").count().as("amountOfDocumentFiles")); 

мне нужно что-то вроде этого: ... .И (USERID) .notInList ("userIdBlackList") ...

[EDIT] Я попробовал этот запрос:

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" 
    ] 
} 

но й (идентификатор пользователя) й ("userIdBlackList"). П (идентификатор пользователь) не имеет никакого эффекта.

[EDIT2]

Я пытался имитировать его в консоли Монго тоже. Я перечислил все documentfiles с помощью команды db.DocumentFile.find() довольно():.

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"} Кто-нибудь знает, что я делаю неправильно?

[EDIT3]

У меня есть эти два документа и с 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" : [ ] 
} 
+0

Пробовали ли вы '$ ne' как' newAggregation (матч (Criteria.where ("ProjectID"). В (ProjectID) .и (USERID). и («userIdBlackList») .ne (userId)), ... '? – chridam

+0

sry, он не работает – quma

+0

Я думаю' и (userId) .and ("userIdBlackList"). ne (userId)) 'should be 'и (" userIdBlackList "). ne (userId))' – Veeram

ответ

0

Вы пробовали использовать .nin

final Aggregation aggregate = newAggregation(
       match(Criteria.where("projectId").in(projectId).and("‌​userIdBlackList").nin(userId)), 
       group("fileName").count().as("amountOfDocumentFiles")); 
Смежные вопросы