Q:

How I can make an query with mongoose from a function using a parameter?

I try make a mongoose query using a function like this:
    /*
     * @param {function} Model - Mongoose Model
     * @param {String} searchText- Text that will be used to search for Regexp
     * @param {String} Key- key to search into a Model
     * @param {object} res - Response of node.js / express
    */

function _partialSearch (Model, searchText, key, res) {
  var search = new RegExp(searchText, "i");

  Model.find({ key : { $regex : search } })
  .exec(function (err, docs) {
    if(err) log(err);
    else {
      res.json(docs);
    }
  })
}

My problem is the query take a parameter key literal and search like this:
I need this:
_partialSearch(Products, 'banana', 'fruts', res)

I spect this:
   Products.find({ 'fruts' : 'banana})

But I get this:
Products.find({ key : 'banana})

A:

Use the bracket notation to create the query object dynamically, so you could restructure your function as follows:
function _partialSearch (Model, searchText, key, res) {
    var search = new RegExp(searchText, "i"),
        query = {};
    query[key] = { $regex : search };

    Model.find(query)
         .exec(function (err, docs) {
            if(err) log(err);
            else {
                res.json(docs);
            }
         });
}

