3
How can I select only 2 items from a array
from inside my Center?
example:
{
"name": "xpto",
"age": 11,
"lista": [{"s": 1}, {"s": 2}, {"s": 3}]
}
I need to get the items you own s
with 2 and 3 where the name
be it xpto
and age
11.
I tried with $and
and $lista.$
but I get a mistake from BadValue
:
{
"$and": [
{"name": "xpto", "age": 11, "lista.s": 2},
{"name": "xpto", "age": 11, "lista.s": 3}
]
},
{
"lista.$": 2
}
What can happen is 2
or 3
does not exist, so would return only a "list", as I can select the 2 from within the array or return only 1 if the 2 do not exist?
The expected result is:
{
"name": "xpto",
"age": 11,
"lista": [{"s": 2}, {"s": 3}]
}
The doubt is
como selecionar apenas 2 itens de um array
, so that you have presented all the items of the array will be returned– RFL
added the expected result to the question to be clearer
– RFL
@RFL got it! In this case, you can edit this object after executing the query. You want to remove objects from the array that
s
is different from 2 or 3?– MatheusPedro
did not want to add complexity to the code since the
mongo
can return the correct values :) You changed your answer and it is now out of scope, I asked how to fetch the items and not how to update my Collection– RFL
@RFL in the previous answer I told you how to fetch items. What you want to do is manipulate the array
lista
. What do you mean, exactly, by fetching the items?– MatheusPedro