Returning the name of the field

11 views
Skip to first unread message

Angel Rodriguez

unread,
Jun 6, 2015, 1:06:14 AM6/6/15
to mongoo...@googlegroups.com
Hello !

Is there any way to return the name of the fields where it was found a search using, for example, conditional "OR" or any other ???

DataBase:
....
{ _id:1, name: 'Angel', lastName: "Rodriguez" },
{ _id:2, name: 'Jose', lastName: "Perez" },
{ _id:3, name: 'Janeth', lastName: "Sanchez" },
{ _id:4, name: 'Carmen', lastName: "Romero" }
....

Query:

dbPeople.find({}).or([{ name: /ne/i }, { lastName: /dr/i }]).exec(callback);

Result:

{ _id:1, name: 'Angel', lastName: "Rodriguez" },
{ _id:3, name: 'Janeth', lastName: "Sanchez" },

Issue....

Document _id:1 matches, because the query found a match in the "lastName" field. How I can know that it was in this field?
Document _id:2 matches, because the query found a match in the "name" field. How I can know that it was in this field?

Thanks !!!

Ryan Wheale

unread,
Jun 8, 2015, 12:28:09 PM6/8/15
to mongoo...@googlegroups.com
Aggregation should be able to handle this. The essential idea is that you would get your initial results (using $match), then you would $project a new field onto the data.  Your new field might be called "matchedField" which would contain the name of the field which was matched.  This would provide the same result as loading your data and looping over it with javascript logic - except you're offloading the processing to the mongo driver which will do it much much faster.  Hope that helps.


--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages