I was researching this issue recently and after a bit of trying out different things I have come here to drop an answer with 3 year old delay:
YES, YOU CAN.
{ partialFilterExpression: { "status": { $in: [null] }, "quantity": { $lt: -1 } } } );
I will be honest, no idea why this works, but it very much works. You'd think this $in query matches only existing fields that have null value but $in: [null]
for some reason will match the same as $exists: false
and you can create a partial index with it.
@Edit: for example in my project those two queries will match exact same results despite technically null
values not being the same as $exists: false
, but for whatever reason when used alongside $in
, they are. For context the field someParticularQuantity
can only have positive values above 0, if the quantity is zero then the field just doesn't exists in the database.
the non-indexable (my old way of querying):
db.collection.find({
$or: [
{
"someParticularQuantity": {
$lt: 4,
},
},
{
"someParticularQuantity": {
$exists: false,
},
},
],
})
completely indexable (the new method I found out):
db.collection.find({
"someParticularQuantity": {
$in: [ null, 1, 2, 3 ],
}
})