Spring数据和mongoDB - 与java列表聚合

时间:2017-01-17 19:46:49

标签: mongodb spring-data aggregation-framework spring-mongodb

我有以下文件(@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();

现在我将以这样的方式扩展聚合,即我只有DocumentFiles,其中userId(在这种情况下&#34; 1234&#34;)不在 userIdBlackList 中。 是否有可能这样做,如伪代码:

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

我需要这样的东西: ....和(userId).notInList(&#34; userIdBlackList&#34;)......

[编辑] 我试过这个问题:

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)。和(&#34; userIdBlackList&#34;)。ne(userId)无效。

[EDIT2]

我也试过在mongo控制台中模拟它。 我已经使用命令 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"
]}

由于这个表达式&#34; userIdBlackList&#34;我预计我没有得到文件文件。 :{&#34; $ ne&#34; :&#34; 587f0d61473c92b933a68ef8&#34;} 有谁知道我做错了什么?

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

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用.nin

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