Reputation: 14851
We have an application where we're storing two types of documents in a Mongo database:
contacts
, which basically represent peoplefilters
, which are essentially a stored MongoDB query that represents a "saved search" for a user.Here's a simplified version of what the data models would look like:
contacts: [
{ id: 1, name: 'Phil', age: 40 },
{ id: 2, name: 'Bob', age: 34 }
]
filters: [
{ query: { name: 'Phil' } }
{ query: { age: { >: 30 } } }
]
Given a filter, it's relatively easy to list all contacts that match that filter:
db.contacts.find(filter.query);
What's harder is finding all filters that match a certain contact. Right now we have something like the following:
matchedFilters = []
_.each(filters, function(filter) {
if (db.contacts.find(_.extend(filter.query, {id: contact_id}).length > 0) {
matchedFilters.push(filter.id)
}
});
Essentially, we need to ask mongo about each filter individually. This results in a huge amount of queries to Mongo.
At the time that we are evaluating this query, we have all the relevant information about the contact we are trying to find. Is there any way to apply the Mongo query syntax to an in-memory Javascript object without needing to ask Mongo about it?
Alternatively, is there a way to ask Mongo to conduct a large number of queries in a single round trip?
Upvotes: 1
Views: 110
Reputation: 51480
Have a look at sift.js. I think, it's exactly what you're looking for.
And here is a blog post about it.
Upvotes: 3