Daksh
Daksh

Reputation: 986

MongoDB $or for two fields inside $match stage

Here's a sample of my MongoDB documents:

{
    "_id" : ObjectId("59caa2660aaafa0016344ce0"),
    "field1" : true,
    "field2" : false
}
{
    "_id" : ObjectId("59caa2690aaafa0016344ce1"),
    "field1" : false,
    "field2" : false
}
{
    "_id" : ObjectId("59caa26c0aaafa0016344ce2"),
    "field1" : false,
    "field2" : true
}

How do I match all documents that have the value of field1 OR the value of field2 OR both field1 and field2 set to true?

In other words, I want to exclude documents where field1 and field2 both are false.

Upvotes: 2

Views: 98

Answers (1)

mickl
mickl

Reputation: 49945

You can use $expr with $or, try:

db.col.find({ $expr: { $or: [ "$field1", "$field2" ] } })

Upvotes: 4

Related Questions