javascript - how do i query a field in mongodb with a boolean value and return a boolean whether its true or false -
check simple collection:
{_id: "01", name: "jimmy", candrive: false }
what want once document
found, manipulate dom , show <div class="driving-tutorial>
if field candrive
value false
, hide if value true
like in meteor:
template.profile.rendered = function() { if (<query>,<field> == true){ $('.driving-tutorial').hide(); else { $('.driving-tutorial').show(); }
you implement logic findone()
method finds first document matches selector/query object in arguments. can call findone()
mongo selector, object specifies required set of attributes of desired document match document. example, selector
var doc = model.findone({ candrive: false });
will match document
{ _id: "01", name: "jimmy", candrive: false }
you can use above logic in template function check existence of document , field, bearing in mind findone()
return null if fails find matching document, happens if document hasn't been loaded yet or has been removed collection:
template.profile.rendered = function() { var doc = model.findone({ candrive: false }); if (doc && !doc.candrive){ $('.driving-tutorial').show(); else { $('.driving-tutorial').hide(); } }
you can use jquery toggle()
method's second version accepts boolean parameter. if parameter true
, matched elements shown; if false
, elements hidden:
template.profile.rendered = function() { var doc = model.findone({ candrive: false }), candrive = (doc && !doc.candrive); $('.driving-tutorial').toggle(candrive); }
Comments
Post a Comment