Gavyn
Gavyn

Reputation: 99

MeteorJs Find all users with an array of user IDs

How can I get every user that has an ID in an array of user IDs. My problem is I have a collection of groups, each group has an array of userIDs called users, these users belong to the group, I need to know how to get the user objects from that array of UserID's whats the best way to do that?

client

export default createContainer((props) => {
  let user = Meteor.users.findOne(Meteor.userId())
  let groupsSub = Meteor.subscribe('groups')

  if (user) {
    let code = user.profile.selectedGroup
    let group = Groups.findOne({code})
    return {
      ready: groupsSub.ready(),
      group,
      users: Meteor.users.find({_id: {$in: group.users}}).fetch()
    }
  }

  return {}
}, HomePage)

sever

 Meteor.publish('groups', function () {
 return Groups.find({
   users: {
     $elemMatch: { $eq: this.userId }
   }
 })
})

and this is where I'm trying to get the users

   render () {
    if (this.props.ready) {
    let group = this.props.group
    let users = this.props.users
    console.log(users)

    return (
      <div className='c-fit-group'>
        <div className='c-fit-group__header'>
          <h1>{group.name}</h1>
        </div>
      </div>
     )
   }

   return <div>Loading...</div>
  }
}

also I'm using React for the front end

when I console.log group.users I get this enter image description here

and when I console.log users I get this enter image description here

Upvotes: 1

Views: 605

Answers (2)

zim
zim

Reputation: 2386

if you simply have an array of the user ids (i'm having trouble telling if that's the issues you're trying to solve), you can use the $in operator. e.g.

let userIds = ['abc', 'def', 'hgi'];
let userCursor = Meteor.users.find({_id: {$in: userIds}});

to just get the results instead of a cursor:

let userResults = Meteor.users.find({_id: {$in: userIds}}).fetch();

update:

from your comment about not getting all the users on the client, is there somewhere where you're actually publishing the users? i think that's the part you're missing.

you can return multiple cursors from a publish, e.g.

Meteor.publish('groups', function () {
    let groupsCursor = Groups.find({
        users: {
            $elemMatch: { $eq: this.userId }
        }
    });

    // find these from... ???
    let userIds = [];

    let usersCursor = Meteor.users.find({_id: {$in: userIds}});

    return [groupsCursor, usersCursor];
});

Upvotes: 3

s7vr
s7vr

Reputation: 75914

You can try below aggregation if you are looking for all users in entire collection.

The initial stage finds all matching userIds in each document and followed by $group to get the distinct users across the collection.

db.collection.aggregate([{
    $project: {
        userIds: {
            $setIntersection: ["$userIds", [1, 2, 3]]
        }
    }
}, {
    $unwinds: "$userIds"
}, {
    $group: {
        $id: null,
        userIds: {
            $addToSet: "$userIds"
        }
    }
}])

Upvotes: 1

Related Questions