Kuryaki
Kuryaki

Reputation: 1011

Query with Type not Equal in 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?

Upvotes: 23

Views: 19020

Answers (1)

Calimero
Calimero

Reputation: 4298

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.

Upvotes: 36

Related Questions