1

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

0

2 Answers 2

3

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];
});
Sign up to request clarification or add additional context in comments.

12 Comments

when I do this I get back an object localCollection.Cursor I have no Idea what that is :P would I expect that to give me an array of user objects?
@GavynCaldwell, no, find() returns a cursor over which you can iterate, if you want. if you just want the results, you can also call fetch. i'll update the answer with an example.
okay I just tried using .fetch() which only returns one of the users. There are at least 2 users on the array
I am super new to mongo and meteor so thank you for the help!!
@GavynCaldwell, i'm unsure why. i just double-checked my code in a browser and it worked as expected -- exactly what i have above except i used real user ids. can you update your question with your code? also, if you're running this code on the client, are you sure you've published all the users you expect to find?
|
1

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"
        }
    }
}])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.