Now i want to find all the documents where StringArray only contains "A" elements. Let's name this the ALL operation.
My current way is: db.TestCollection.find({ StringArray: { $elemMatch: {$eq: "A"}, $not: {$elemMatch: {$ne: "A"}}}})
which makes sure that i only hit the documents where StringArray contains 'A' AND does not contain anything which is not 'A'. (Without the first part it would also match empty arrays).
Now this works for the simple cases, for more complicated elemMatch expressions (regex, other logical operators) this is still possible, but gets complicated to build the appropriate negated expression.
It gets really complicated, if I have multiple nested ALL operations (e.g. find all document where all the embedded documents all contain the string array above)
Has anyone a better idea on how to do this ALL operation without the need to manually build the negated expression?
Thanks, Torni
Asya Kamsky
unread,
Jun 6, 2015, 1:14:26 AM6/6/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongodb-user
How about using aggregation framework? Multiple possibilities exist...
That works to find all documents where StringArray contains one or
more of any single letter. If you specifically want "A" you can use [
"A" ] as second argument to $setDifference and then match results that
are 0 only.