Mongo DB query with variable parameters

I am trying to query a mongo collection with filters that vary according to the user's need. According to the parameters I do the construction of an object with the push JSON function to define the params.criteria.

The problem is that Mongo does not receive JSON objects to query I get this message:

message: BasicBSONList can only work with numeric keys, not: [$or]
code: 0

How can I build these parameters, or how can I convert the object to Mongo's BSON format?

*As the number of parameters varies I can not build the Mongo query in a fixed way like:

params.criteria = {
"parameter1": {
"$eq": parameter1
},
"parameter2": {
"$eq": parameter2
},
"parameter3": {
"$regex": parameter3
}
};
1 person has
this question
+1
Reply
  • Camilo,

    I think the basic question here - is what is the structure of your table / Collection you are querying ? Your syntax looks basically correct and you can pass an object as a where clause.

    Can you be specific on the structure of your table (Names of columns - and the values you are passing to the database engine - and their type). ?

    Also - for equality - no need to say $eq - I would amend your example object above to this:

    var params= {}

    params.criteria = {
    "parameter1": parameter1,
    "parameter2": parameter2,
    "parameter3": {
    "$regex": parameter3
    }
    };

    Bruce
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Finally - I would also ask the quick question - have you attempted your 'where' object on the database / Collection you are interested in - in the Query builder - on the table you are trying to query - before running it inside your application - like such:

    Click Link to view:

    http://prntscr.com/j8te57
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • Bruce thanks for your response,

    My issue is not the collection, my issue is building the params.criteria since these vary according to the filters that the user selects (its hard to explain, Ill try).

    For example if the user requires 1 filter, the params.criteria should be:

    params.criteria = {
    "parameter1": {
    "$operator": parameter1
    }
    };


    If the user requires 2 filters, the params.criteria should be:

    params.criteria = {
    "parameter1": {
    "$operator": parameter1
    },
    "parameter2": {
    "$operator": parameter2
    }
    };


    I want to build the params.criteria array dynamically.

    Initially I create an object and push the search filters:

    var paramsObject = [];

    if (parameter1 !== "0") {
    searchFilter1 = {
    "dbColumn": {
    "$operator": searchFilter1
    },
    };
    paramsObject.push(searchFilter1);
    }


    At this point the object will be

    [{
    "dbColumn": {
    "$operator": searchFilter1
    },
    }];


    The problem is that Mongo does not receive JSON objects [] as params.criteria only {} so I get this message:

    message: BasicBSONList can only work with numeric keys, not: [$or]
    code: 0

    How can I build dynamically these params.criteria, or how can I convert my object to Mongo's BSON format?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • Camilio,

    a couple of things.

    1. You should not be sending a paramsObject.

    2. Params.criteria can be an object.

    3. You can build the params.criteria object on the fly, and you are correct - you cannot add to an object - by doing a push - as that creates an array (not an object).

    4. In order to perform the above work ( I presume you are doing this in server code ) - your code would be :

    params = {}.
    params.criteria = {} // add the 'column to the object params called 'criteria' - 'on the fly' simply by referencing it....

    // your condition....

    if (parameter1 > " ") {
    params.criteria.dbcolumn1 : searchfilter1
    }; // where you have an $eq - this works as well - and is quicker to type. Again - simply reference the column name in the object to add it to the object.

    if (parameter2 > " ") {
    params.criteria.dbcolumn2 : {
    "$operator": searchFilter2
    }
    }; // where you have a $ne $gte $regex etc.

    Simply reference the field in an object to 'create' it.
    etc.....

    Best,

    Bruce
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned