1

I'm dont think that this is a Meteor specific question, but rather around mongo and building mongo queries.

If I have the following structure,

{
  username : someName,
  contacts : [
     {
      userid : asdfae33rtqqxxx,
      name : contactName,
      status : friend
     }
     {
      userid : asdfae33rtqqxxx,
      name : anotherName,
      status : pending
     }
     {
      userid : asdfae33rtqqxxx,
      name : contactName,
      status : blocked
     }
  ]
}

How could I pass in values from this array into a query against the users collection, to a) get the users in this array, or b) get all users in this array from the users collection with a particular status.

If this is not possible, how should I adjust my schema in order to make these sorts of queries possible?

3
  • I think you should make another collection named contacts with username as relation key Commented Dec 27, 2014 at 14:18
  • I don't think thats the right solution for my use case, also email address is the only natural primary key I'm using, but while Meteor seems to work well with some normalisation (keeping publications / subscriptions smaller) I'd prefer not to fully normalize my data. Commented Dec 27, 2014 at 18:26
  • In a related question, how could I find the array element of contacts if I search for status : pending? Commented Oct 1, 2015 at 16:03

1 Answer 1

1

This function will return a Meteor.users cursor based on an array of contacts and an optionally required status:

var usersByContacts = function(contacts, requiredStatus) {
  var userIds = _.chain(contacts)
    .map(function(c) {
      if (requiredStatus) {
        if (c.status === requiredStatus)
          return c.userid;
      } else {
        return c.userid;
      }
    })
    .compact()
    .value();

  return Meteor.users.find({_id: {$in: userIds}});
};

You can use it like this:

var users1 = usersByContacts(thing.contacts);
var users2 = usersByContacts(thing.contacts, 'pending');

This assumes thing has the schema referenced in your question. Also note that if you find this is a common pattern in your code, you should consider turning usersByContacts into a transform.

Sign up to request clarification or add additional context in comments.

2 Comments

great tip dave, thanks, I was originally really trying to find out if there was a native way to use those values in the mongo query itself, but this is a great solution and pretty straight forward. cheers. I'm going to leave the question open for a day or so, but I'm going to go ahead and use this, and will definitely have a look at transforms.
Great - I'm glad this was helpful. Mongo doesn't have the notion of joins, so I'm pretty sure this is the only way to do it.

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.