Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a boolean field in a mongodb

Tags:

mongodb

meteor

I like to initially save data to a new mongodb in my Meteor App. The collection is a available an the following code works. But decision.visble is created as String, though I would like to have it as boolean. How do I pass this information? Is this done via the insert?

Client

  var decision = {};
  decision.visble = 'false';

  Meteor.call('addDecision',decision);

Sever

 'addDecision':function(decision){
    return Decision.insert(decision);
 }

EDIT

Just found a kind of answer for me:

The type seems to be take automatically. So when I leave out the quotes and pass only false instead of 'false' I get a boolean type instead of a sting.

But there must be a smarter procedure. Here is a List of BSON types

which seem to be used in an $type operator. So finally remains the question:

How do I correctly define the datatypes I want to store in a collection?

like image 826
Michael Hoeller Avatar asked Dec 01 '25 16:12

Michael Hoeller


1 Answers

You assigned the visible field using the string syntax "false" so its JS type will be String.

You simply need to make the field into a Boolean using the plain false keyword :

decision.visible = false;

EDIT :

How do I correctly define the datatypes I want to store in a collection?

You could use something like simple-schema : https://github.com/aldeed/meteor-simple-schema

like image 104
saimeunt Avatar answered Dec 03 '25 13:12

saimeunt