matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
438 stars 68 forks source link

projection #561

Closed frankapimenta closed 7 years ago

frankapimenta commented 7 years ago

Where can I control the projection of the query?

to do something like this: http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection

matteodem commented 7 years ago

You can adjust the selector config, see docs

frankapimenta commented 7 years ago

can I set the query as an aggregate like this? db.profiles.aggregate([{'$unwind': '$posts'}, {'$match': {'posts.tags': {$in: ['post1']}}}, {'$group': {'_id': '$_id','posts': {'$push': '$posts'}}}, {$limit: 1}])

matteodem commented 7 years ago

you have to call the aggregate method outside of easy-search

frankapimenta commented 7 years ago

thank you for the reply. is there any example in the docs?

matteodem commented 7 years ago

No, as it's not part of what easy search tries to resolve

frankapimenta commented 7 years ago

Maybe I was not able to explain well what I want to do.

I need that because I would like to search by tags of posts. Posts are stored in an array in the profiles collection.

const Profiles = new Meteor.Collection('profiles');
const Profile = Class.create({
  name: 'Profile',
  collection: Profiles,
  fields: {
    posts: {
      type: [Object] // {title: '...', tags: [...], ...}
    }
  }
});

Applying (like in your example of email addresses) $elemMatch is going to return 1 match only even thought there are more. Applying $in will return the profile itself.

Is there a way of doing it by using easy search?

thank you for your answers so far! :)

matteodem commented 7 years ago

Can you show me how you would achieve that without easy-search?

frankapimenta commented 7 years ago

with a publication returning the aggregation I pasted here before (one can then skip, limit...whatever).

matteodem commented 7 years ago

You can adjust the selector argument by using the selector configuration that's documented here http://matteodem.github.io/meteor-easy-search/docs/engines/. as to the aggregate call, that has to be done outside of easy-search.

frankapimenta commented 7 years ago

I have tried to change the selector before this issue.

When I use $elemMatch it will return just 1 element (post) even though there are many other posts matching the query.

Using $in will return the profiles that have posts with those queried tags.

There is one way to do it that is to create a collection for Posts but I wanted to first squeeze this option first.