在Springboot Mongo Aggregation中将ObjectID转换为字符串

时间:2020-01-29 11:29:21

标签: string mongodb spring-boot aggregation-framework objectid

我有一个查询:

db.results.aggregate([{
        $project: {
            "_id": {
                $toString: "$_id"
            }
        }
    }
]);

我需要将其转换为java springboot聚合。我对Java中的$ toString感到困惑。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

在Spring聚合中,您需要像下面这样使用,它给出一个ObjectId的String值:

ProjectionOperation projectOp = Aggregation.project().andArrayOf(new Document("$toString","$_id")).as("id");
相关问题