node.js - return part of the doc using $or in mongoose mongodb -


user.find({         $or:[             {'usersetdata.name': { $regex: new regexp( searchterm ,'i') }},             {'local.email': { $regex: new regexp( searchterm ,'i') }},             {'google.name': { $regex: new regexp( searchterm ,'i') }},             {'google.email': { $regex: new regexp( searchterm ,'i') }},             {'facebook.name': { $regex: new regexp( searchterm ,'i') }},             {'facebook.email': { $regex: new regexp( searchterm ,'i') }}         ]     }, function(err, doc) {         if( err ){             console.log( err );         }         return doc;     }); 

the above query fetches user email or name matches searched criteria. expected returns entire doc of user found..

http://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ on mongodb site demonstrate use of projection limit fields of doc returned, eg:

db.inventory.find( { type: 'food' }, { item: 1, qty: 1, _id:0 } ) 

is possible combine $or mongoose projection option of mongo, or post processing option?

you should able pass object find , use projections

user.find({     $or:[         {'usersetdata.name': { $regex: new regexp( searchterm ,'i')         }},         {'local.email': { $regex: new regexp( searchterm ,'i') }},         {'google.name': { $regex: new regexp( searchterm ,'i') }},         {'google.email': { $regex: new regexp( searchterm ,'i') }},         {'facebook.name': { $regex: new regexp( searchterm ,'i') }},         {'facebook.email': { $regex: new regexp( searchterm ,'i') }}     ] }, {<projection goes here>},  function(err, doc) {     if( err ){         console.log( err );     }     return doc; }); 

source http://mongoosejs.com/docs/api.html#model_model.find


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -