node.js - Only search mongoose model for keys in schema -


i'm building search filter results mongoose model. filter options except 2 need queried model1. other 2 query results in model2.

before added these last 2 options, had search working so:

var filteroptions = req.body;     for(var key in filteroptions) {     if(filteroptions.hasownproperty(key)) {         filteroptions[key] = new regexp(filteroptions[key], "i");     } }  model1.find(filteroptions, function(err, results) {     //use results search model2. }) 

i'm getting stuck how can specify query model1 keys in model. (or, if there's way edit filteroptions object not include last 2 options.) thanks.

there many ways it, utility framework lodash or underscore can done in few lines of code:

// require lodash first var _ = require('lodash');  (...)  // lodash way of doing transformation regexp objects var filteroptions = _.transform(req.body, function(result, value, key) {   result[key] = new regexp(value, "i"); });  // keys of model1 schema var model1keys = _.keys(model1.schema.paths);   // create copy of filteroptions keys of model1     var model1filter = _.pick(filteroptions, model1keys);  // create copy of filteroptions without keys of model1 var model2filter = _.omit(filteroptions, model1keys);   model1.find(model1filter, function(err, results) {     (....) }) 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -