问题
My database object has an optional property named tags
which is a string array. I want to write a query that returns objects if they match one of these conditions:
- They don't have a
tags
property. - They have a tags property that has at least one item included in another array called
queryTags
From reading the documentation I came up with the following but it doesn't work:
let query = {
tags: { '$or': [{'$exists': false}, {'$in': queryTags}]}
}
回答1:
$or
is a top-level operator, so your query needs to be:
let query = {
'$or': [{tags: {'$exists': false}}, {tags: {'$in': queryTags}}]
}
来源:https://stackoverflow.com/questions/51403361/mongodb-filter-by-array-property-only-if-it-exists