0

I have the following document (@Document):

@Id
private String id;
private String fileName;
private String projectId;
private List<DocumentFileVersion> documentFileVersions;
private List<String> userIdBlackList; // here userIds are included

and this is my current aggregation:

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

Now I will extend the aggreagation in that way that I only will have the DocumentFiles where userId (in this case "1234") is not in userIdBlackList. Is there a possibility to do that, like in pseudocode:

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

I would need something like this: ... .and(userId).notInList("userIdBlackList") ...

[EDIT] I have tried this query:

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

A Database entry can look like this:

{
"_id" : ObjectId("587e7cabafdaff28743f3034"),
"_class" : "com.smartinnotec.legalprojectmanagement.dao.domain.DocumentFile",
"fileName" : "Hydrangeas.jpg",
"projectId" : "587e7c95afdaff28743f302e",
"userIdBlackList" : [
    "5589929b887dc1fdb501cdbb"
    ]
}

but .and(userId).and("‌​userIdBlackList").ne(userId) has no effect.

[EDIT2]

I have tried to simulate it in the mongo console too. I have listed all documentfiles with the command 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"
]}

and my query looks like this:

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

I have expected that I do not get a documentfile because of this expression "‌​userIdBlackList" : { "$ne" : "587f0d61473c92b933a68ef8"} Does anyone know what I am doing wrong?

[EDIT3]

I have this two documents and with the aggegate:

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

I get the amount of 2 but it should 1. I don't know what I am doing wrong?

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" : [ ]
}
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
quma
  • 5,233
  • 26
  • 80
  • 146
  • Did you try `$ne` as `newAggregation(match(Criteria.where("projectId").in(projectId).and(userId).and("userIdBlackList").ne(userId)),...`? – chridam Jan 17 '17 at 20:05
  • sry, it does not work – quma Jan 17 '17 at 20:53
  • I think `and(userId).and("‌​userIdBlackList").ne(userId))` should be `and("‌​userIdBlackList").ne(userId))` – s7vr Jan 17 '17 at 21:00
  • it still not works – quma Jan 17 '17 at 21:03
  • are you sure ? I ran this generated query from aggregation `db.collection.aggregate({ "$match" : { "projectId" : { "$in" : [ "587e7c95afdaff28743f302e"]} , "‌​userIdBlackList" : { "$ne" : "somestring"}}} , { "$group" : { "_id" : "$fileName" , "amountOfDocumentFiles" : { "$sum" : 1}}});` it returns `{ "_id" : "Hydrangeas.jpg", "amountOfDocumentFiles" : 1 }` – s7vr Jan 17 '17 at 21:29

1 Answers1

0

Have you tried using .nin

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