Sample document
[{
"_id": "1111",
"name": "Dani"
},
{
"_id": "2222",
"name": "Guya",
"address": "Arlozorov",
"city": "Tel Aviv"
}]
Expected output, i want to add the length field
[{
"_id": "1111",
"name": "Dani",
"length": 2
},
{
"_id": "2222",
"name": "Guya",
"address": "Arlozorov",
"city": "Tel Aviv",
"length": 4
}]
2
Answers
Query
$$ROOT
is the document (system variable)$set
Playmongo
There’s a fair amount of ambiguity in your description of
"length"
. For example, if there is an array, does that count as one, or should the array contents be counted too? Same for embedded/nested fields documents, etc. And should the ever present"_id"
field be counted?Anyway, given your example documents and desired output, here’s one way you could
update
each document with your"length"
field.Try it on mongoplayground.net.