2

How can I assign an array to a variable. I am able to return the documents that I want but not able to specifically get all the elements in the array inside a particular field. This field is called 'likes.'

My query to filter documents I want:

Posts.find({}, {owner: Meteor.user()})

I would like to retrieve all the elements inside an array field called 'likes' from the Posts collection. (Each element in the 'likes' field is an object ID)

I tried with various operators such as $all and $in of the 'likes' field and tested within console.log, but I cant manage to get the Ids. According to the mongo docs, I need to specify an element within the operator but I don't want that. I simply want anything that is inside.

var likers = Posts.find({}, {owner: Meteor.user()}, {likes: {$in: [] }})

Basically, Im trying to retrieve all the elements from the 'likes' field so I can make another query to return results.

I am running meteor 0.9+

3 Answers 3

5

Give this a try:

var myPosts = Posts.find(
  {owner: Meteor.user()},
  {fields: {likes: 1}}
).fetch()

var myLikes = _.chain(myPosts)
  .pluck('likes')
  .flatten()
  .uniq()
  .value();

First we fetch all of the posts where the current user is the owner. Each post will contain an _id and a likes array. Next we extract all of the likes by:

  1. Plucking out each likes from each document. This gets us an array of arrays.
  2. Flatten the array of arrays into a single array of likes.
  3. Run uniq so we have only the unique likes (not necessary but could be an optimization).

You can then use myLikes in a subsequent query. For example:

Likes.find({_id: {$in: myLikes}})

Here is some test data in case myPosts is empty:

var myPosts = [
  {_id: 1, likes: [1, 2, 3]},
  {_id: 2, likes: [2, 3, 4]},
  {_id: 3, likes: [4, 5, 6]}
];

In this case myLikes will be: [ 1, 2, 3, 4, 5, 6 ]

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

3 Comments

Hi David, I have tried this. After putting the myLikes variable into the console.log and into another query, there is no value returned.
It may be that myPosts is an empty array for you because the documents don't exist or they were not published. Assuming I got the right data structure, the myLikes code is correct. I added some test data to the bottom of my answer to verify.
@DavidWeldon THANKS! I have been searching for this for months!
1

You can specify which fields you want your cursor to contain, so you can limit it to the likes field:

var allLikes = Posts.find({
  owner: Meteor.user()
}, {
  fields: {likes: 1},
}).fetch();

This will give you an array of all posts, where each post contain only the likes array:

[{
  likes: ['A', 'B', 'C'],
}, {
  likes: ['D', 'E'],
}];

etc. Now, I understand you want a single array with all the objectIDs listed there. For this you can transform the array with underscore. There are numerous ways to obtain the result, for example:

var likes = _.reduce(allLikes, function(memo, post) {
  return memo.concat(post.likes);
}, []);

2 Comments

Each post will only have one array. Inside it is a list of object ids. So the underscore method is not necessary but thanks for pointing it out I can keep in mind for future. From a single array such as the one you wrote ['A', 'B', 'C'] I would like this in a variable AllLikes. Then, query another collection Forms.find({_id: allLikes});
Essentially, I am trying to retrieve all the values inside the array of the 'likes' field. Then use these values to query another collection.
0

I just realised, in the Posts.find query, I had to insert an empty criteria for it to work. So the change resulted to the following.

var myPosts = Posts.find(
{},
{owner: Meteor.user()},
{fields: {likes: 1}}
).fetch()

Either way, I could not have come to this result without your input so plus one for you David!

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.