Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query with Type not Equal in mongodb

Tags:

mongodb

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?

like image 227
Kuryaki Avatar asked Dec 23 '13 12:12

Kuryaki


People also ask

Is not equal in MongoDB?

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.

How do I use not condition in MongoDB?

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.

What is $NE in MongoDB?

$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 .

How can check data type field in MongoDB?

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.


1 Answers

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.

like image 159
Calimero Avatar answered Sep 21 '22 15:09

Calimero