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; });
Comments
Post a Comment