Spring/MongoDB多计数,用于唯一查询

huangapple go评论86阅读模式
英文:

Spring/MongoDB Multi count for unique query

问题

这是您要翻译的内容:

我必须将此查询翻译为计算每个类别的所有公共和私有库的数量:

"$group" : {
  _id:"$category",
  nbPublic:{ $sum: { $cond: { if: "$public", then: 1, else: 0 }}},
  nbPrivate:{ $sum: { $cond: { if: "$public", then: 0, else: 1 }}}
}

以获得以下类型的结果:

{ cat1: { nbPublic: 3, nbPrivate: 5 } },
{ cat2: { nbPublic: 12, nbPrivate: 3 } },
....

我尝试过:

Aggregation aggregation = newAggregation(
            group("category").count().as("nbPublic"),
            match(Criteria.where("public").is(true)),
            group("category").count().as("nbPrivate"),
            match(Criteria.where("public").is(false)),
            project("nbPublic").and("category").previousOperation().and("nbPrivate").previousOperation());
    
    AggregationResults<Dest.class> result = mongoTemplate.aggregate(
            aggregation, Model.class, Dest.class
    );

我应该怎么做?

谢谢您的帮助 Spring/MongoDB多计数,用于唯一查询

英文:

I have to translate this query to count all public and private libaries for each category:

&quot;$group&quot; : {
  _id:&quot;$category&quot;,
  nbPublic:{ $sum: { $cond: { if: &quot;$public&quot;, then: 1, else: 0 }}},
  nbPrivate:{ $sum: { $cond: { if: &quot;$public&quot;, then: 0, else: 1 }}}
}

to get that type of result :

{ cat1: { nbPublic: 3, nbPrivate: 5 } },
{ cat2: { nbPublic: 12, nbPrivate: 3 } },
....

I tried :

Aggregation aggregation = newAggregation(
            group(&quot;category&quot;).count().as(&quot;nbPublic&quot;),
            match(Criteria.where(&quot;public&quot;).is(true)),
            group(&quot;category&quot;).count().as(&quot;nbPrivate&quot;),
            match(Criteria.where(&quot;public&quot;).is(false)),
            project(&quot;nbPublic&quot;).and(&quot;category&quot;).previousOperation().and(&quot;nbPrivate&quot;).previousOperation());

    
    AggregationResults&lt;Dest.class&gt; result = mongoTemplate.aggregate(
            aggregation, Model.class, Dest.class
    );

What should I do ?

Thx for your help Spring/MongoDB多计数,用于唯一查询

答案1

得分: 0

你可以使用mongoTemplate来实现这个功能。

@Autowired
MongoTemplate mongoTemplate;

然后你可以像这样进行操作。

public List<Object> test() {
    Aggregation aggregation = Aggregation.newAggregation(
        a -> new Document("$group",
                new Document("_id", "$category")
                        .append("nbPublic",
                                new Document("$sum",
                                        new Document("$cond",
                                                new Document("$if", "$public")
                                                        .append("then", 1)
                                                        .append("else", 0)
                                        )
                                )
                        )
                        .append("nbPrivate",
                                new Document("$sum",
                                        new Document("$cond",
                                                new Document("$if", "$public")
                                                        .append("then", 0)
                                                        .append("else", 1)
                                        )
                                )
                        )
        )
    ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

    return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}

注意:我没有测试过。但是这应该能够工作。这是一种将Mongo脚本转换为Spring的“技巧”。无论如何,我会查找并告诉你关于聚合操作的信息。

更新 1:这可能是简单的方法

group("category")
.sum(ConditionalOperators.when("public").then(1).otherwise(0)).as("nbPublic")
.sum(ConditionalOperators.when("public").then(0).otherwise(1)).as("nbPrivate")
英文:

You can achieve this with mongoTemplate

@Autowired
MongoTemplate mongoTemplate;

Then you can do something like this.

public List&lt;Object&gt; test(){
	Aggregation aggregation = Aggregation.newAggregation(
		a-&gt; new Document(&quot;$group&quot;,
				new Document(&quot;_id&quot;,&quot;$category&quot;)
				.append(&quot;nbPublic&quot;, 
					new Document(&quot;$sum&quot;, 
						new Document (&quot;$cond&quot;,
							new Document(&quot;$if&quot;, &quot;$public&quot;)
							.append(&quot;then&quot;,1)
							.append(&quot;else&quot;,0)
						)
					)
				)
				.append(&quot;nbPrivate&quot;, 
					new Document(&quot;$sum&quot;, 
						new Document (&quot;$cond&quot;,
							new Document(&quot;$if&quot;, &quot;$public&quot;)
							.append(&quot;then&quot;,1)
							.append(&quot;else&quot;,0)
						)
					)
				)
		)

	).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

	return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}

Note : I've not tested. But this should work. this is a kind of trick to convert mongo script to spring. Anyhow I'll find and let you know about the Aggregation operations.

Update 1 : This may be easy way

group(&quot;category&quot;) 
.sum(ConditionalOperators.when(&quot;public&quot;).then(1).otherwise(0)).as(&quot;nbPublic&quot;) 
.sum(ConditionalOperators.when(&quot;public&quot;).then(0).otherwise(1)).as(&quot;nbPrivate&quot;)

huangapple
  • 本文由 发表于 2020年9月17日 22:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定