D
D
driver4582017-07-27 03:33:02
MongoDB
driver458, 2017-07-27 03:33:02

How to select documents from MongoDB when one of the fields is passed empty?

<select class="form-control" id="sel1" ng-model="$ctrl.search.vendor" ng-change="$ctrl.find()">
    <option value="">Все</option>
    <option ng-repeat="vendor in $ctrl.vendors">{{vendor}}</option>
</select>

return Doc.find(search);
From this code, when I select show "all", it looks for documents where the field is strictly empty. How to fix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
driver458, 2017-07-28
@driver458

From Html, I was unable to pass an object or regular expression as a value. No matter how I try, at the output value is perceived as a string. I came up with a "so-so" solution, the main thing is that it works) Already inside the script I go through the object and recursively delete empty fields. I'm sure there is a better solution to this problem.

self.delEmptyFields = function(obj) {
    for (var key in obj) {
        var value = obj[key];
        if (value === "" || value === null) {
            delete obj[key];
        } else if (Object.prototype.toString.call(value) === '[object Object]') {
            self.delEmptyFields(value);
        } else if ($.isArray(value)) {
            for (var k in value) {
                self.delEmptyFields(value[k]);
            }
        }
    }
}

P
Philipp, 2017-07-27
@zoonman

https://stackoverflow.com/questions/21467697/how-d...
https://docs.mongodb.com/manual/reference/operator...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question