meteor-easy-search icon indicating copy to clipboard operation
meteor-easy-search copied to clipboard

Searching in related fields with $lookup

Open matteodem opened this issue 7 years ago • 0 comments

describe how to search in related fields with $lookup.

import { Meteor } from 'meteor/meteor'

export const findRelatedFieldDocuments = Meteor.wrapAsync((data, cb) => {
  const {
    collection,
    from,
    localField,
    foreignField,
    as,
    selector,
  } = data

  collection.rawCollection().aggregate([
    {
      $lookup: {
        from,
        localField,
        foreignField,
        as,
      },
    },
    { $match: selector },
  ]).toArray().then(res => cb(null, res)).catch(err => cb(err))
})

Not really performant, but still worth mentioning.

matteodem avatar Nov 17 '17 11:11 matteodem