I have no ideas how to do this, so I need your advice. I have form with 4 checkboxes:
<input type="checkbox" class="checkbox" id="check_stain">
<input type="checkbox" class="checkbox" id="check_black">
<input type="checkbox" class="checkbox" id="check_titan">
<input type="checkbox" class="checkbox" id="check_special">
I send data with simple submit, so if check_stain was checked, then req.body.check_stain will be "on", if not - "undefined".
In my db I have collection "companies", with several objects, every of which contains such schema:
{...
"stain": "false",
"black": "true",
"titan": "false",
"special": "true",
...}
values are simple strings. So, I need to get every object for which specified checkboxes are true (on), but not including ones that nones (undefined). In other words, I'd like to create a simple search algorithm. Let me give you an example: we check check_stain and we should get every object where stain is "true". Maybe other values will be "true"/"false" (doesn't matter). If we select check_stain and check_black, we get object with stain & black = "true", other fields are not interested.
I always find with such code:
collection.find({ %my-condition% } ,function(err, companies) {
companies.each(function(err, company){
//do smth with found
});
});
But I have no idea how to write condition to find. I suppose, I should use $or operator in request. I read the docs, but I still can't get it. Thanks.
UPDATE: Huh, that seems to be a solution of my problem:
if (req.body.sort_query == 'req-steel') {
var condition = {}
if (req.body.check_stain == "on") {condition['stain'] = 'true';}
if (req.body.check_black == "on") {condition['black'] = 'true';}
if (req.body.check_titan == "on") {condition['titan'] = 'true';}
if (req.body.check_other == "on") {condition['special'] = 'true';}
for (var i in condition) {
if (condition[i] != "true") {
delete condition[i];
}
}
var companies_list = new Array();
collection.find(condition, function (err, companies) {
companies.each(function (err, company) {
//do smth
});
});
};
For example black and stain are true:
var condition = {
"stain": "false",
"black": "true",
"titan": "false",
"special": "true",
}
for (var i in condition) {
if (condition[i] != "true") {
delete condition[i];
}
}
collection.find(condition ,function(err, companies) {
companies.each(function(err, company){
//do smth with found
});
});