将mongoDB脚本转换为Spring引导

将mongoDB脚本转换为Spring引导,spring,mongodb,spring-boot,mongotemplate,Spring,Mongodb,Spring Boot,Mongotemplate,我正在使用mongoDb和spring boot。我已经实现了一对多关系。我将视频存储在一个集合中,并将其\u id存储在类别中,该类别是卷轴集合中的嵌入对象 卷轴收藏 { _id:"reelId", category:[ { _id:"catId_1", videos:[ { _id:"video_1",

我正在使用mongoDb和spring boot。我已经实现了一对多关系。我将视频存储在一个集合中,并将其\u id存储在类别中,该类别是卷轴集合中的嵌入对象

卷轴收藏

{
    _id:"reelId",
    category:[
        {
            _id:"catId_1",
            videos:[
                {
                    _id:"video_1",
                    student: 40
                },
                {
                    _id:"video_2",
                    student: 30
                }
            ]
        },
        // second catgeory object
    ]
}
{
    _id"video_1",
    title:"first",
    description:"des 1",
    isGlobal: true
},
{
    _id"video_2",
    title:"second",
    description:"des 2",
    isGlobal: false
}
视频采集

{
    _id:"reelId",
    category:[
        {
            _id:"catId_1",
            videos:[
                {
                    _id:"video_1",
                    student: 40
                },
                {
                    _id:"video_2",
                    student: 30
                }
            ]
        },
        // second catgeory object
    ]
}
{
    _id"video_1",
    title:"first",
    description:"des 1",
    isGlobal: true
},
{
    _id"video_2",
    title:"second",
    description:"des 2",
    isGlobal: false
}
预期结果。

{
    _id:"reelId",
    category:[
        {
            _id:"catId_1",
            videos:[
                {
                    _id:"video_1",
                    title:"first",
                    description:"des 1",
                    isGlobal: true
                    student: 40
                },
                {
                    _id:"video_2",
                    title:"second",
                    description:"des 2",
                    isGlobal: false
                    student: 30
                }
            ]
        },
        // second catgeory object
    ]
}
我已经写了一个mongo脚本,正如我所期望的那样工作得很好

db.getCollection('reel').aggregate([
{ $unwind:{path:"$category"} },
{ $unwind:{path:"$category.videos"} },
{ $lookup :{ from:"video", localField:"category.videos._id", foreignField:"_id", as:"data"} },
{ $unwind:{path:"$data", preserveNullAndEmptyArrays: true } },
{ $addFields: { "category.videos.title":"$data.title","category.videos.description":"$data.description" }},
{ $group: { _id:{_id:"$_id", category:"$category._id"}, videos:{$push:"$category.videos"} } },
{ $group: { _id:"$_id._id", category:{ $push: { _id:"$_id.category", videos: "$videos"} } } }
])

但是由于我是mongo的新手,我不明白如何将这个脚本映射到java。我尝试了很多方法,但没有成功。提前谢谢

我想,你可以试试这个

插入这些导入

import org.springframework.data.mongodb.core.aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.unwind;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
这就是方法

public Object findAllwithVideos() {

        LookupOperation lookupVideos = LookupOperation
                .newLookup().from("video").localField("category.videos._id").foreignField("_id").as("data");
        Aggregation aggregation = Aggregation.newAggregation(
                unwind("category"),
                unwind("category.videos"),
                lookupVideos,
                unwind("data", true),
                new AggregationOperation() {
                    @Override
                    public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                        return new Document("$addFields",
                                new Document("category.videos.title", "$data.title")
                                        .append("category.videos.description", "$data.description")
                        );
                    }
                },
                group(
                        Fields.from(
                                Fields.field("_id", "$_id"),
                                Fields.field("category", "$category._id")
                        )
                ).addToSet("$category.videos").as("videos"),
                group("_id._id")
                        .addToSet(new BasicDBObject("_id", "$_id.category")
                                .append("videos", "$videos")).as("category")


        ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
        return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Reel.class), Object.class);
    }

我想,你可以试试这个

插入这些导入

import org.springframework.data.mongodb.core.aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.unwind;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
这就是方法

public Object findAllwithVideos() {

        LookupOperation lookupVideos = LookupOperation
                .newLookup().from("video").localField("category.videos._id").foreignField("_id").as("data");
        Aggregation aggregation = Aggregation.newAggregation(
                unwind("category"),
                unwind("category.videos"),
                lookupVideos,
                unwind("data", true),
                new AggregationOperation() {
                    @Override
                    public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                        return new Document("$addFields",
                                new Document("category.videos.title", "$data.title")
                                        .append("category.videos.description", "$data.description")
                        );
                    }
                },
                group(
                        Fields.from(
                                Fields.field("_id", "$_id"),
                                Fields.field("category", "$category._id")
                        )
                ).addToSet("$category.videos").as("videos"),
                group("_id._id")
                        .addToSet(new BasicDBObject("_id", "$_id.category")
                                .append("videos", "$videos")).as("category")


        ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
        return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Reel.class), Object.class);
    }

感谢您在mongo中尝试而不是在网上发布问题..感谢您在mongo中尝试而不是在网上发布问题。。