im doing the following query which work perfectly and brings me the part of the documents that complies the type filter
db.users.find({phoneNumber:/^\+/, devices:{$exists:true, $type:3}});
However im trying to get all the devices that exists but are not type 3 (object), i could go trying each type [http://docs.mongodb.org/manual/reference/operator/query/type/], but wanted to know if it is possible to negate type 3 i tried
db.users.count({phoneNumber:/^\+/, devices:{$exists:true, $type: { $neq: 3}}});
db.users.count({phoneNumber:/^\+/, devices:{$exists:true, $type: { $not: 3}}});
db.users.count({phoneNumber:/^\+/, devices:{$exists:true, $type: { $not: {$eq:3}}}});
But all of them throw the same exception
exception: type not supported for appendMinElementForType
Any clues on how to do this query?
In MongoDB, the $ne operator signifies the not-equal operator. It is one of the comparison operators in MongoDB. It is used to match the documents whose field value is not equal to the one specified by the $ne operator.
MongoDB provides different types of logical query operators and $not operator is one of them. This operator is used to perform logical NOT operation on the specified operator expressions and select or retrieve only those documents that do not match the given operator expression.
$ne selects the documents where the value of the field is not equal to the specified value . This includes documents that do not contain the field .
As described above, the $type operator works on the BSON type in MongoDB, and it offers two identifiers for each BSON type; one is “integer” and the other is “string“. For instance, to locate a Double data type, one can use integer value “1” and a string “double” to locate the Double data type in the specified field.
This should work as expected :
{phoneNumber:/^\+/ ,$and:[{devices:{$exists:true}},{devices:{$not:{$type:3}}}]}
This note from the mongodb docs might be of interest to understand what's going on here (copied from MongoDB reference docs - $and operator manual page - emphasis is mine):
MongoDB provides an implicit AND operation when specifying a comma separated list of expressions. Using an explicit AND with the $and operator is necessary when the same field or operator has to be specified in multiple expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With