I'm trying to convert the "interesse" array into a new object with 2 fields "term" which is the current array terms and a statistic value with the name "str" with a starting value of 0
as the following example. my database currently has about 16KK entries so i'm trying to create some sort of function or routine to deal with it.
Original documents
{
"email" : "qwert@yahoo.com",
"interesse" : [
"Calcados",
"Acessorios para viagem",
"Interesse em noticias sobre curiosidades"
],
"data" : "2017-01-30"
}
{
"email" : "asert@hotmail.com",
"interesse" : [
"Eletrodomesticos e portateis",
"Geladeiras"
],
"data" : "2017-01-30"
}
Result documents
{
"email" : "qwert@yahoo.com",
"interesse" : [
{"term":"Calcados","str":0},
{"term":"Acessorios para viagem","str":0},
{"term":"Interesse em noticias sobre curiosidades","str":0}
],
"data" : "2017-01-30"
}
{
"email" : "asert@hotmail.com",
"interesse" : [
{"term":"Eletrodomesticos e portateis","str":0},
{"term":"Geladeiras","str":0},
],
"data" : "2017-01-30"
}
I was looking at arrayToObject.. but the function only transforms the entire array into a single object... what i want is to transform each value into new object...
You need $addFields to replace existing field and $map to transform your current array:
db.collection.aggregate([
{
$addFields: {
interesse: {
$map: {
input: "$interesse",
in: {
term: "$$this", str: 0
}
}
}
}
}
])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.