how do i pass boolean values created in python to mongodb?

  • Last Update :
  • Techknowledgy :

Are you inserting the documents from mongo shell? Mongo shell won't accept 'False' or 'True' as boolean values. You should use a Python Mongo driver client. Pymongo should work. Check the below sample:

import pymongo
client = pymongo.MongoClient('localhost', 27017)
db = client.testdatabase
col = db.testcollection
col.insert({
   'FixedH': False,
   'Mstereo': True,
   'RecMet': False,
   'Sstereo': True,
   'bond': False,
   'charge': False,
   'isotope': False,
   'length': 223,
   'nocomponents': 1,
   'nolayers': 6,
   'stereo': True
})
cursor = col.find()
print 'Found', cursor.count()
print cursor.next()
client.close()

Suggestion : 2

What is the syntax for leading bang! in JavaScript function?,What is the syntax for defining the data structure in C++?,What is the syntax for input parameters (variables) in a MySQL query?,Following is the query for boolean values −

You can use $eq as well as $ne operator for boolean values. Let us first create a collection with documents −

> db.booleanQueryDemo.insertOne({
   "UserName": "John",
   "UserAge": 23,
   "isMarried": true
}); {
   "acknowledged": true,
   "insertedId": ObjectId("5cc815c08f9e6ff3eb0ce44a")
} >
db.booleanQueryDemo.insertOne({
   "UserName": "Chris",
   "UserAge": 21,
   "isMarried": false
}); {
   "acknowledged": true,
   "insertedId": ObjectId("5cc815d08f9e6ff3eb0ce44b")
} >
db.booleanQueryDemo.insertOne({
   "UserName": "Robert",
   "UserAge": 24,
   "isMarried": false
}); {
   "acknowledged": true,
   "insertedId": ObjectId("5cc815dc8f9e6ff3eb0ce44c")
} >
db.booleanQueryDemo.insertOne({
   "UserName": "David",
   "UserAge": 26,
   "isMarried": true
}); {
   "acknowledged": true,
   "insertedId": ObjectId("5cc815ed8f9e6ff3eb0ce44d")
}

Following is the query to display all documents from a collection with the help of find() method −

> db.booleanQueryDemo.find().pretty();

This will produce the following output −

{
   "_id": ObjectId("5cc815c08f9e6ff3eb0ce44a"),
   "UserName": "John",
   "UserAge": 23,
   "isMarried": true
} {
   "_id": ObjectId("5cc815d08f9e6ff3eb0ce44b"),
   "UserName": "Chris",
   "UserAge": 21,
   "isMarried": false
} {
   "_id": ObjectId("5cc815dc8f9e6ff3eb0ce44c"),
   "UserName": "Robert",
   "UserAge": 24,
   "isMarried": false
} {
   "_id": ObjectId("5cc815ed8f9e6ff3eb0ce44d"),
   "UserName": "David",
   "UserAge": 26,
   "isMarried": true
}

Following is the query −

> db.booleanQueryDemo.find({
   isMarried: {
      $ne: false
   }
}).pretty();

Suggestion : 3

Last Updated : 24 Dec, 2021,GATE CS 2021 Syllabus

Command to install express-validator:

npm install express - validator