简体   繁体   English

如何使用 $toLower 和 $trim 聚合运算符使用 MongoDB Java?

[英]How to use $toLower and $trim aggregation operators using MongoDB Java?

I have a collection called Users, which contains an array called skills.我有一个名为 Users 的集合,其中包含一个名为 Skills 的数组。

I use the following code to unwind the skills array and count the number of documents associated with each skill:我使用以下代码展开技能数组并计算与每个技能关联的文档数:

Bson uw = unwind("$skills");
Bson sbc = sortByCount("$skills");
Bson limit = limit(10);
coll.aggregate(Arrays.asList(uw, sbc,limit)).forEach(printDocuments());

Now I want to make use of $trim and $toLower operations for the above aggregation because in the database, some skills saved in different ways (eg, "CSS" , "CSS " , and "css" ).现在我想对上述聚合使用$trim$toLower操作,因为在数据库中,一些技能以不同的方式保存(例如, "CSS""CSS""css" )。

I'm able to do this in the mongo shell with the following aggregation:我可以通过以下聚合在 mongo shell 中执行此操作:

db.users.aggregate([{$unwind:"$skills"} , {$sortByCount:{$toLower:{$trim:{input:"$skills"}}}}])

But I'm having troubles with implementing it in Java.但是我在 Java 中实现它时遇到了麻烦。

Do you have any idea?你有什么主意吗?

I managed to find a way to do this with changing the sortByCount to the following:我设法找到一种方法来将 sortByCount 更改为以下内容:

Bson sbc = sortByCount(eq("$toLower", eq("$trim", eq("input", "$skills"))));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM