结构:
String str = "11020199,Abc Germany ,aduz,,444,bieb,dc,2 ,2222.00,whatever 5dc,222.22,22.00,\"\"98,00\"\",\"\"12,00\"\",21-09-2018,06:00";
// Replace the comma between double quotes with a replacement char you're sure isn't in the String:
// TODO: Use a more suitable character, I don't know what your text can/cannot contain
String modifiedStr = str.replaceAll("(\"\"[^,]+),([^,]+\"\")", "$1$2");
// Now split by comma:
String[] array = modifiedStr.split(",");
// And then change the replacement char back again to a comma:
for(int i=0; i<array.length; i++)
array[i] = array[i].replace("", ",");
我想在我的文件字段的每个数组元素中添加计算字段(Number),并将其值设置为同一元素(即类型)中的其他字段。 我已经尝试过使用聚合框架,但是每次我设置值时,它都会从整个数组中获取所有值,即:
"entity": [
{
"_id": {
"$oid": "5bbf6e6d69a634eeb5a32a0e"
},
"Extension Id": "PR00027173",
"Files": [
{
"Locale": "en_WW",
"FileExtension": "ai",
"Number": []
},
{
"Locale": "sv_SE",
"Type": "Technical drawing",
"FileExtension": "eps",
"Number": []
}
]
},
{
"_id": {
"$oid": "5bbf6e6d69a634eeb5a32b7c"
},
"Extension Id": "PR00027174",
"Files": [
{
"Locale": "en_WW",
"FileExtension": "ai",
"Number": []
},
{
"Locale": "sv_SE",
"Type": "Technical drawing",
"FileExtension": "eps",
"Number": []
}
]
}
]
是否可以将新字段Number值设置为FileExtension,但只能从同一元素而不是整个数组? 这就是我的投影总的样子:
"Files": [
{
"Locale": "en_WW",
"FileExtension": "ai",
"Number": [
"ai",
"eps"
]
},
{
"Locale": "sv_SE",
"Type": "Technical drawing",
"FileExtension": "eps",
"Number": [
"ai",
"eps"
]
}
]
答案 0 :(得分:0)
这对于标准投影是不可能的,因为它是一个数组。您必须使用数组运算符$map
来设置数组元素。
类似
db.collection.aggregate([
...other stages..
{"$addFields":{
"Files":{
"$map":{
"input":"$Files",
"in":{
"Locale": "$$this.Locale",
"FileExtension": "$$this.FileExtension",
"Number":"$$this.FileExtension"
}
}
}
}}
])