Hi, I have some data that looks like this:
{
_id: "someID"
array1: [
{ _id: "id1", array2: [
{_id: "a", other_data: "data"},
{_id: "b", other_data: "data"},
{_id: "c", other_data: "data"}
]
}.
{ _id: "id2", array2: [
{_id: "d", other_data: "data"},
{_id: "e", other_data: "data"},
{_id: "a", other_data: "data"}
]
}.
{ _id: "id3", array2: [
{_id: "x", other_data: "data"},
{_id: "y", other_data: "data"},
{_id: "z", other_data: "data"}
]
}
]
}
I wish to find all elements in array1 which do not have an element in array2
with the ID of “a”. So, I wish to retrieve an array as such, since only id3
's array2
field does not contain an element with _id
“a”.
[
{
_id: "id3"
}
]
If I repeat this for searching for “b”, I should get the following since only id2
and id3
do not contain “b”:
[
{
_id: "id2"
},
{
_id: "id3"
}
]
If I search for “q”, I should get the following since they all do not contain “q” in array2
:
[
{
_id: "id1"
},
{
_id: "id2"
},
{
_id: "id3"
}
]
I tried db.findOne({ _id: mainID, "array1.array2._id" : { $ne: selectedID } }))
but this isn’t giving me the expected result.
How should I go about this?
Any help is appreciated, thank you!