mongodb - 在 spring data mongo 中将 ObjectId 项目到 String

标签 mongodb spring-data-jpa aggregation-framework

您好,以下是我的文档 “用户”

{
    "_id" : ObjectId("5dcab69fc6355e16d8164bd3"),
    "username" : "kellen.swift",
    "firstName" : "Kellen",
    "lastName" : "Swift",
    "authorities" : [],
    "active" : true,
    "_class" : "...User"
}

“评论”

{
    "_id" : ObjectId("5dcac8fc75de953b5c3025cb"),
    "content" : "Wonderful, thanks for the explanation",
    "videoId" : "5dcab83475de951cc80dd2f0",
    "userId" : "5dcab69fc6355e16d8164bd3",
    "active" : true,
    "_class" : "...Comment"
}

我正在使用以下查询撰写用户评论:

db.users.aggregate([
        {
            "$project": {
                "_id": {
                    "$toString": "$_id"
                },
                "username": 1,
                "firstName": 1,
                "lastName": 1,
            }
        },
        {
            $lookup: {
                    from: "comments",
                    localField: "_id",
                    foreignField: "userId",
                    as: "user_comments"
            }
        },
        {
            $unwind: "$user_comments"
        },
        {
            $project:{
                username: 1,
                name: {
                    $concat: ["$firstName", " ", "$lastName"]
                },
                content: "$user_comments.content",
                videoId: "$user_comments.videoId"
            }
        }
]);

这使用 Robo3t 给了我一个正确的结果。

当我尝试从 Spring 数据接口(interface)执行相同的命令时,我无法获得确切的结果。

以下是我的java代码:

private void getAllUserCommentsForVideo(String userId) {

        ProjectionOperation userProjections = Aggregation.project().andInclude("username", "firstName", "lastName");


        LookupOperation lookup = Aggregation.lookup(
                "comments",
                "_id",
                "userId",
                "userComments"
        );

        ProjectionOperation userCommentProject = Aggregation.project(
                "username"
        ).andInclude("userComments.content");

        Aggregation aggregation = Aggregation.newAggregation(
                userProjections,
                lookup,
                Aggregation.unwind("userComments"),
                userCommentProject
        );

        List<BasicDBObject> users = mongoTemplate.aggregate(aggregation, "users", BasicDBObject.class).getMappedResults();
        for (BasicDBObject object : users) {
            System.out.println(object.toJson());
        }

    }

从查询中,我可以看到 _id 没有正确映射,我在这里做错了什么吗?

请告诉我成功执行此操作所需的任何修改。

谢谢。

最佳答案

尝试创建自定义聚合操作以正确地将 ObjectId 映射到字符串:

AggregationOperation projectionOperation = context -> {
    Document toString = new Document("$toString", "$_id");
    Document id = new Document("_id", toString);
    Document project = new Document("$project", id);

    return project;
};

关于mongodb - 在 spring data mongo 中将 ObjectId 项目到 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58840867/

相关文章:

mongodb - 尝试访问 `http://127.0.0.1:27017/` 时出错 + 运行 `mongod` 时出现多个错误

MongoDB将数字转换为科学计数法的字符串

蒙戈数据库 : how to use aggregate to build standings on nested fields

angularjs - 使用 Angular JS 将数据插入 MongoDB

mongodb - 即时定义 Mongoose Schema

java - 如何编写具有多个连接的 Spring Data JPA 规范?

java - 使用 Spring Data 查询 Redis 中的嵌套对象

java - ManyToMany 本身与 AssociationTable 和 EmbeddedId - Spring-Data-Jpa

regex - 在 mongodb 聚合中的 $expr 中使用 $regex

node.js - 使用 Node.js 进行 Azure 媒体服务/处理媒体上传和流媒体