gpt4 book ai didi

mongodb - $concat string with $cond in mongodb aggregation

转载 作者:可可西里 更新时间:2023-11-01 10:46:14 30 4
gpt4 key购买 nike

[
{
"user_id" : 12453,
"profile_type" : "demo_type_1",
"records" : [
{
"type" : "typ_11",
"value" : {
"high" : 115,
"low" : 78
},
"_meta" : {
"data_type" : "text"
}
},
{
"type" : "type_1",
"files" : [
{
"title" : "path_prescription_1",
"url" : "/file_name.extension"
},
{
"title" : "path_prescription_2",
"url" : "/file_name__1.extension"
}
],
"_meta" : {
"data_type" : "file"
}
}
]
},
{
"user_id" : 12455,
"profile_type" : "demo_type_1",
"records" : [
{
"type" : "typ_11",
"value" : {
"high" : 115,
"low" : 78
},
"_meta" : {
"data_type" : "text"
}
},
{
"type" : "type_1",
"files" : [
{
"title" : "path_prescription_1",
"url" : "/file_name.extension"
},
{
"title" : "path_prescription_2",
"url" : "/file_name__1.extension"
}
],
"_meta" : {
"data_type" : "file"
}
}
]
},
...
]

我想在 url 值前添加前缀,使其成为绝对路径,同时仅当 _meta 字段数据类型为文件而不是文本时才从数据库中检索。以上述格式存储,并仅使用附加的 url 以相同的格式检索。

有没有办法通过聚合管道来做到这一点?

最佳答案

您可以像这样尝试使用 $addFields:

db.getCollection('test').aggregate(

[

{
$unwind: {
path:"$records",

preserveNullAndEmptyArrays: true
}
},

{
$unwind: {
path:"$records.files",

preserveNullAndEmptyArrays: true
}
},

{
$addFields: {
"records.files.url":{
$cond: {
if: {
$eq: ['$records.files', undefined]
},
then: null,
else: {$concat: ["some_prefix", "$records.files.url"]}
}

}
}

}

]

)

这会给你:

/* 1 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "typ_11",
"value" : {
"high" : 115.0,
"low" : 78.0
},
"_meta" : {
"data_type" : "text"
},
"files" : {
"url" : null
}
}
}

/* 2 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_1",
"url" : "some_prefix/file_name.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}

/* 3 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508520"),
"user_id" : 12453.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_2",
"url" : "some_prefix/file_name__1.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}

/* 4 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "typ_11",
"value" : {
"high" : 115.0,
"low" : 78.0
},
"_meta" : {
"data_type" : "text"
},
"files" : {
"url" : null
}
}
}

/* 5 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_1",
"url" : "some_prefix/file_name.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}

/* 6 */
{
"_id" : ObjectId("5b6c484b9a8ea6a11c508521"),
"user_id" : 12455.0,
"profile_type" : "demo_type_1",
"records" : {
"type" : "type_1",
"files" : {
"title" : "path_prescription_2",
"url" : "some_prefix/file_name__1.extension"
},
"_meta" : {
"data_type" : "file"
}
}
}

但是当记录类型为 text 时,请记住忽略 records.files.url 字段。

关于mongodb - $concat string with $cond in mongodb aggregation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51765947/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com