0

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
    });
});
};
4
  • Where's the problem? Just create an object with the keys which are true and leave the others out. Commented Jun 17, 2012 at 11:40
  • not sure I've understand you correctly. Where should I create a new object, in database with collection.insert() or where? As I got it, you suggest to send this object as parameter for finding request or what? Commented Jun 17, 2012 at 11:45
  • stain should be "true": collection.find({ stain: "true" }) stain & black should be "true": collection.find({ stain: "true", black: "true" }) Commented Jun 17, 2012 at 11:54
  • well, I know, but how can I create a condition if I don't know which values are true? I shouldn't include false values in request, as you understand. The right will be smth like collection.find({ if(check_stain) stain: "true", if(check_black) black: "true" .... }) But as you know, I can't do such requests... Commented Jun 17, 2012 at 12:01

1 Answer 1

1

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
    });
});

Live condition DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

thank you, give me a few moments to examine you code. I still cant get how it works...
Just remove all keys which are not true from the condition object.
I've updated my post. Here's a code I've written. I've crossed my fingers and launched it :) Huh, it seems to be working properly :) Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.