仅当值存在汇总的mongodb时创建一个集合
我有一个数据集:
[
{
"_id": 1,
"Data": {
"a": {
"levela": {
"fname": "fname",
"lname": "lname"
},
"levelfacility": []
}
}
},
{
"_id": 2,
"Data": {
"a": {
"levela": {},
"levelfacility": [
{
"facility": "facility"
}
]
}
}
},
{
"_id": 3,
"Data": {
"a": {
"levela": {},
"levelfacility": []
}
}
}
]
仅在存在时,我想将上限应用于值,但是当我应用$ SET时,如果不存在,它只是一个空字符串。
[
{
"_id": 1,
"Data": {
"a": {
"levela": {
"fname": "fname",
"fnameNORM": "FNAME",
"lname": "lname"
},
"levelfacility": []
}
}
},
{
"_id": 2,
"Data": {
"a": {
"levela": {},
"levelfacility": [
{
"facility": "facility",
"facilityNORM": "FACILITY"
}
]
}
}
},
{
"_id": 2,
"Data": {
"a": {
"levela": {},
"levelfacility": []
}
}
}
]
我想应用类似$有条件的东西,所以我不带有原始字段的新字段,
我尝试了一个字符串
{
$set: {
"Data.a.levela.fnameNORM": {
$cond: {
if: {
"$Data.a.levela.fname": {
$exist: true
}
},
then: {
$toUpper: "$Data.a.levela.fname"
},
else: "$$REMOVE"
}
}
}
}
,我已经尝试了此数组
{
$set: {
"Data.a.levelfacility": {
$map: {
input: "$Data.a.levelfacility",
in: {
$mergeObjects: [
"$$this",
{
"facilityNORM": {
$cond: {
if: {
"$$this.FacilityName": {
$exist: true
}
},
then: {
$toUpper: "$$this.FacilityName"
},
else: "$$REMOVE"
}
}
}
]
}
}
}
}
}
I have a dataset:
[
{
"_id": 1,
"Data": {
"a": {
"levela": {
"fname": "fname",
"lname": "lname"
},
"levelfacility": []
}
}
},
{
"_id": 2,
"Data": {
"a": {
"levela": {},
"levelfacility": [
{
"facility": "facility"
}
]
}
}
},
{
"_id": 3,
"Data": {
"a": {
"levela": {},
"levelfacility": []
}
}
}
]
I want to apply an upper case to the values only if exists but when I apply a $set, it just is an empty string if it doesn't exist.
[
{
"_id": 1,
"Data": {
"a": {
"levela": {
"fname": "fname",
"fnameNORM": "FNAME",
"lname": "lname"
},
"levelfacility": []
}
}
},
{
"_id": 2,
"Data": {
"a": {
"levela": {},
"levelfacility": [
{
"facility": "facility",
"facilityNORM": "FACILITY"
}
]
}
}
},
{
"_id": 2,
"Data": {
"a": {
"levela": {},
"levelfacility": []
}
}
}
]
I want to apply something like a $conditional so I am not left with new fields with the original field doesnt exist
I have tried this for a string
{
$set: {
"Data.a.levela.fnameNORM": {
$cond: {
if: {
"$Data.a.levela.fname": {
$exist: true
}
},
then: {
$toUpper: "$Data.a.levela.fname"
},
else: "$REMOVE"
}
}
}
}
I have tried this for an array
{
$set: {
"Data.a.levelfacility": {
$map: {
input: "$Data.a.levelfacility",
in: {
$mergeObjects: [
"$this",
{
"facilityNORM": {
$cond: {
if: {
"$this.FacilityName": {
$exist: true
}
},
then: {
$toUpper: "$this.FacilityName"
},
else: "$REMOVE"
}
}
}
]
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上在正确的轨道上。只需使用
$ MAP
即可处理Levelfacity
数组。这是 mongo playground 供您参考。
You are actually on the right track. Just use
$map
to handlelevelfacility
array.Here is the Mongo playground for your reference.