Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax for MongoDB query for boolean values?

Tags:

mongodb

The values for the key is_agent is TRUE or blank.

I have the following query:

db.users.find( { $not: {is_agent:TRUE} }, {email:1} )

I get the following error:

ReferenceError: TRUE is not defined

When I used "TRUE" I got the following error:

"$err" : "Can't canonicalize query: BadValue unknown top level operator: $not",
"code" : 17287

What is the correct syntax?

like image 853
DBWeinstein Avatar asked Apr 16 '15 02:04

DBWeinstein


People also ask

What is boolean MongoDB?

Boolean is a native field type in BSON (MongoDB's server-side storage format, aka "Binary JSON"). Booleans use less storage than an integer or string and avoid any unexpected side effects of comparison.

How is boolean stored in MongoDB?

I know there are three ways of storing the boolean data in mongodb: 0 or 1 as a String. 0 or 1 as a Number. True or False as a Boolean.

Does MongoDB support boolean?

String in MongoDB must be UTF-8 valid. Integer − This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. Boolean − This type is used to store a boolean (true/ false) value.

What syntax does MongoDB use?

MongoDB queries are based on JavaScript. The language is reasonably easy to learn and many tools are available to query MongoDB data using SQL syntax.


1 Answers

$eq Matches values that are equal to a specified value.

$ne Matches all values that are not equal to a specified value.

Documentation on mongodb operators

Example:

db.users.find({is_agent: {$ne: true}})

or

db.users.find({is_agent: {$eq: true}})
like image 60
mate64 Avatar answered Sep 28 '22 10:09

mate64