I want a query in MongoDB that does the following:
- A document returned
profession.organizationproperty can be the string 'bank'. - A document returned
profession.cityproperty can be the string 'NY'. - However, when the document has 'bank' for 'profession.organization' AND 'NY' for 'profession.city' the document must be excluded.
To sum up a document can have city 'NY' or organization 'bank' but must be excluded if it has these properties at the same time.
What I have tried so far.
targetObj = await db.db(MDBC.db).collection(MDBC.pC)
.aggregate([{
$match: {
$and: [
{ 'profession.organization': { $ne: 'bank' } },
{ 'profession.city': { $ne: 'NY' } }
]
}
}, { $sample: { size: 1 } }]).next();