1

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,,,

1 Answer 1

1

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

2 Comments

this is first time i use $in. learn a lot and it's work... thank You Sir,,,
@KarinaL No worries, glad you learned something new useful :)

Your Answer

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