How to dynamically build MongoDB Query with multiple OR?

和自甴很熟 提交于 2019-12-11 03:44:52

问题


I would like to be able to dynamically build a Mongo query using Node with multiple OR's. The point of this query is to do an AND search in the database with multiple search terms.

For example, suppose someone wanted to search for records containing the words "dog" and "cereal." The query would only return records that have "dog" and "cereal" somewhere in the record (i.e. the search terms can appear in different columns).

I know that the query should end up looking like this:

query = {
    $and: [
        {$or: [
            {col1: {$regex: "first", $options: "i"}},
            {col2: {$regex: "first", $options: "i"}}
        ]},
        {$or: [
            {col1: {$regex: "second", $options: "i"}},
            {col2: {$regex: "second", $options: "i"}}
        ]}
    ]
}

My node code, however, does not produce this. This is the closest I have gotten to figuring this out:

var regexQueryWrapper = {};
regexQueryWrapper["$and"] = [];
var wrapper = {};
wrapper["$or"] = [];
var query = {};
searchTerms.forEach(function(term) {
    searchFields.forEach(function(field) {
        query[field] = {$regex: term, $options: 'i'};
        wrapper["$or"].push(query);
    });
    regexQueryWrapper["$and"].push(wrapper);
});

What is the best way to do this? Is there a different approach that I am missing?


回答1:


You're close. You seem to be reusing objects. Here's some adjusted code that builds the query you're looking for, with the temporary objects reinitialized on each forEach iteration:

var regexQueryWrapper = {};
regexQueryWrapper["$and"] = [];

searchTerms.forEach(function(term) {

    var wrapper = {"$or": []};

    searchFields.forEach(function(field) {
        var query = {};
        query[field] = {$regex: term, $options: 'i'};
        wrapper["$or"].push(query);
    });

    regexQueryWrapper["$and"].push(wrapper);

});

And an alternative implementation using map:

var query = {"$and": searchTerms.map(function(term) {

    return {"$or": searchFields.map(function(field) {

        var o = {};
        o[field] = {"$regex": term, "$options": "i"};
        return o;

    })};

})};



回答2:


I think you over complicated this.

Build your query step by step, by thinking about the the fact you are manipulating mostly two nested arrays (and and or).

With a function that pushes an array and another one that returns an array, NodeJS code would be:

query = {and: []};

function add_and(or_query) {
 query["and"].push({or: or_query});
}

function build_or_query(term) {
 return [
   {col1 : {$regex: term, $options: "i"}},
   {col2: {$regex: term, $options: "i"}}
 ]
}

Rename and use those functions as you see fit in your code to manage user input events. You can also merge them if you want to, ensuring very compact code:

function add_and(term) {
 query["and"].push({or: [
   {col1 : {$regex: term, $options: "i"}},
   {col2: {$regex: term, $options: "i"}}
 ]});
}

Use like (two functions) :

add_and(build_or_query('test1'));
add_and(build_or_query('test2'));

Or like (one function):

add_and('test1');
add_and('test2');

Result:

{ 
  and: [ 
    { or: [ { col1: { '$regex': 'test1', '$options': 'i' } },
            { col2: { '$regex': 'test1', '$options': 'i' } } ] },
    { or: [ { col1: { '$regex': 'test2', '$options': 'i' } },
            { col2: { '$regex': 'test2', '$options': 'i' } } ] } 
  ]
}


来源:https://stackoverflow.com/questions/45068245/how-to-dynamically-build-mongodb-query-with-multiple-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!