user4804299
user4804299

Reputation:

Meteor: How to find array of object with matched array of selector?

Suppose I have array of string:

var lcArray = ["this", "is", "array", "of", "title"]

I need to find all object with lC field match with one of lcArray. I did this:

var qC = _.forEach(lcArray, function(lc) {
  MyCollection.find({lC:lc}, {
    fields: {lC:1, title:1, src:1, sC:1, _id:0},
    reactive: false,
    transform: null
  }).fetch();
});
console.log(qC);  // return ["this", "is", "array", "of", "title"]

I need this output:

[
    {
        lC: "array", title: "Array", src:"cat", sC:"cat"
    },
    {
        lC: "title", title: "Title", src:"bear", sC:"bear"
    }
]

How to find array of object with matched array of selector? thank You,,,

Upvotes: 1

Views: 262

Answers (1)

chridam
chridam

Reputation: 103365

Use the $in operator in your query as it selects the documents where the value of a field equals any value in the specified array:

var lcArray = ["this", "is", "array", "of", "title"];
var qC = MyCollection.find({"lC" : { "$in": lcArray } }, {
    fields: {lC:1, title:1, src:1, sC:1, _id:0},
    reactive: false,
    transform: null
}).fetch();

console.log(qC);  

Upvotes: 1

Related Questions