Mongo text search score keeps disappearing

I’m doing a full text search in Mongo on the server side then publishing the results to the user. Using Meteor Toys, I can see the message docs updated with a score field, but for convo and user docs, I see the score field briefly appear then disappear.

Not sure why the score field keeps vanishing in the Convo and Meteor.Users collections.

My code for the publish function is below.

Meteor.publish(SEARCH_RESULTS, function ({teamId, text}) {
   check(arguments[0], {
     teamId: String,
     text: String
   });

   const _getMsgs = () => {
    const selector = {
      convoId: { $in: convoIds },
      $text: { $search: text }
    };
    const options = {
      fields: { score: { $meta: 'textScore' } },
      sort: { score: { $meta: 'textScore' } }
    };
    return Messages.find(selector, options);
  };

  const _getConvos = () => {
    const selector = {
      userIds: userId,
      $text: { $search: text }
    };
    const options = {
      fields: { score: { $meta: 'textScore' } },
      sort: { score: { $meta: 'textScore' } }
    };
    return Convos.find(selector, options);
  };

  const _getUsers = () => {
    const selector = {
        $or: [
          {[`roles.${teamId}`]: 'admin'},
          {[`roles.${teamId}`]: 'member'}
        ],
      $text: { $search: text }
    };
    const options = {
      fields: {
        score: {$meta: 'textScore'},
        username: 1,
        emails: 1,
        profileImageUrl: 1
      },
      sort: { score: { $meta: 'textScore' } }
    };
    return Meteor.users.find(selector, options);
  };

  return [ _getMsgs(), _getConvos(), _getUsers() ];
}
1 Like