Nestjs中不同语言数据的mongodb聚合代码

发布于 2025-01-10 09:46:25 字数 933 浏览 0 评论 0 原文

我的数据库

{
    "_id": {
        "$oid": "ddfsfs"
    },
    "id": 3,
    "parent_id": 6,
    "translation":
       [
        {
        "language": "en",
        "desc": "prod detail",
        "name": "example1"
        },
        {
        "language": "tr",
        "desc": "detaylar",
        "name": "ornek1"
        }
       ]
}

我想做这个: 例如,仅选择英语字段。如果它的最终形式只有英文字段中的名称和描述,可以吗?以及如何?

{
    "_id": {
        "$oid": "ddfsfs"
    },
     "id": 3,
     "parent_id": 6,
     "desc": "prod detail",
     "name": "example1"   
}

或者数据库可以是这样的。再次仅保留英语字段,

{
    "_id": {
        "$oid": "62189ffd81f6b6bb05d8d409"
    },
    "id": 7,
    "parent_id": 8,
    "en": {
        "desc": "hii",
        "name": "serial ethernet"
    },
    "tr": {
        "desc": "merhaba",
        "name": "seri ethernet"
    }
}

请与我分享用于 Nestjs 的 mongodb 聚合代码。谢谢你!:)

my database

{
    "_id": {
        "$oid": "ddfsfs"
    },
    "id": 3,
    "parent_id": 6,
    "translation":
       [
        {
        "language": "en",
        "desc": "prod detail",
        "name": "example1"
        },
        {
        "language": "tr",
        "desc": "detaylar",
        "name": "ornek1"
        }
       ]
}

I want to make this :
For example, only the English field is selected. Is it okay if it has only the name and description in the English field in its final form? and How?

{
    "_id": {
        "$oid": "ddfsfs"
    },
     "id": 3,
     "parent_id": 6,
     "desc": "prod detail",
     "name": "example1"   
}

or the database can be like this. again only the english field will remain

{
    "_id": {
        "$oid": "62189ffd81f6b6bb05d8d409"
    },
    "id": 7,
    "parent_id": 8,
    "en": {
        "desc": "hii",
        "name": "serial ethernet"
    },
    "tr": {
        "desc": "merhaba",
        "name": "seri ethernet"
    }
}

please share me mongodb aggregations code for use in nestjs. thank you!:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美羊羊 2025-01-17 09:46:25

对于第一个数据库模式,您需要一个管道,首先使用 $filter 运算符。这将返回一个数组,其中仅包含满足过滤条件的文档。因此表达式:

{ $filter: {
    input: '$translation',
    cond: { '$eq': ['$this.language', 'en'] }
} }

产生结果

[
    {
        "language" : "en",
        "desc" : "prod detail",
        "name" : "example1
    }
]

第二步是将上述文档与您需要的顶级字段合并,在本例中,您希望 _id、id 和parent_id 字段与上述字段合并。使用 $mergeObjects为此。

$mergeObjects 需要文档作为输入,需要使用 $arrayElemAt< /a> 或 $first 运算符从前面的数组中获取文档 $filter 表达式,即

{ $arrayElemAt: [ 
    { $filter: {
        input: '$translation',
        cond: { '$eq': ['$this.language', 'en'] }
    } }, 
    0 
] } 

$first

{ $first: {
    $filter: {
        input: '$translation',
        cond: { '$eq': ['$this.language', 'en'] }
    } 
} } 

将返回

{
    "language" : "en",
    "desc" : "prod detail",
    "name" : "example1
}

现在您可以使用表达式

{
    $mergeObjects: [ 
        { _id: "$_id", id: '$id', parent_id: "$parent_id" },  
        { $arrayElemAt: [ 
            { $filter: {
                input: '$translation',
                cond: { '$eq': ['$this.language', 'en'] }
            } }, 
            0 
        ] } 
    ] 
}

来获取

{
    "_id" : ObjectId("6218abb521d9a0dfecd02e4b"),
    "parent_id" : 6.0,
    "language" : "en",
    "desc" : "prod detail",
    "name" : "example1"
}

最后一步是将根替换为运算符 $replaceRoot 和您的最终聚合管道尝试以下聚合管道 (MongoDb Playground) 应该如下所示

{ $replaceRoot: { 
    newRoot: { 
        $mergeObjects: [ 
            { _id: "$_id", id: '$id', parent_id: "$parent_id" },  
            { $arrayElemAt: [ 
                { $filter: {
                   input: '$translation',
                   cond: { '$eq': ['$this.language', 'en'] }
                } }, 
                0 
            ] } 
        ] 
    } 
} } 

对于替代模式,运行此 聚合管道 只需将文档与 en 键合并即可

{ $replaceRoot: { 
    newRoot: { 
        $mergeObjects: [ 
            { _id: "$_id", id: '$id', parent_id: "$parent_id" },  
            '$en'
        ] 
    } 
} } 

For the first database schema, you need a pipeline that will first filter the translation array using the $filter operator. This will return an array with just the document that satisfies the filtering condition. So the expression:

{ $filter: {
    input: '$translation',
    cond: { '$eq': ['$this.language', 'en'] }
} }

yields the result

[
    {
        "language" : "en",
        "desc" : "prod detail",
        "name" : "example1
    }
]

The second step will be to merge the above document with the top-level fields you need, in this case you want the _id, id and parent_id fields to combine with the above. Use $mergeObjects for this.

Since $mergeObjects requires documents as input, you need to use $arrayElemAt or $first operator to get the document from the array in the previous $filter expression, i.e.

{ $arrayElemAt: [ 
    { $filter: {
        input: '$translation',
        cond: { '$eq': ['$this.language', 'en'] }
    } }, 
    0 
] } 

or $first

{ $first: {
    $filter: {
        input: '$translation',
        cond: { '$eq': ['$this.language', 'en'] }
    } 
} } 

will return

{
    "language" : "en",
    "desc" : "prod detail",
    "name" : "example1
}

Now you can use the expression

{
    $mergeObjects: [ 
        { _id: "$_id", id: '$id', parent_id: "$parent_id" },  
        { $arrayElemAt: [ 
            { $filter: {
                input: '$translation',
                cond: { '$eq': ['$this.language', 'en'] }
            } }, 
            0 
        ] } 
    ] 
}

to get

{
    "_id" : ObjectId("6218abb521d9a0dfecd02e4b"),
    "parent_id" : 6.0,
    "language" : "en",
    "desc" : "prod detail",
    "name" : "example1"
}

Final step will be to replace the root with the operator $replaceRoot and your final aggregate pipeline Try the following aggregate pipeline (MongoDb Playground) should look like this

{ $replaceRoot: { 
    newRoot: { 
        $mergeObjects: [ 
            { _id: "$_id", id: '$id', parent_id: "$parent_id" },  
            { $arrayElemAt: [ 
                { $filter: {
                   input: '$translation',
                   cond: { '$eq': ['$this.language', 'en'] }
                } }, 
                0 
            ] } 
        ] 
    } 
} } 

For the alternative schema run this aggregation pipeline where it's only a matter of merging the document with the en key i.e.

{ $replaceRoot: { 
    newRoot: { 
        $mergeObjects: [ 
            { _id: "$_id", id: '$id', parent_id: "$parent_id" },  
            '$en'
        ] 
    } 
} } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文