I have multiple collections in which every record has a "tags" field. For example:
Collection A:
{name: "1", tags: "foo, bar"}
{name: "2", tags: "foo"}
Collection B:
{name: "3", date: "today", tags: "bar"}
And now I want to use the tags of one record to search all collections for matching results. For example, when I do a search on A (tags: "foo, bar"), ideally I'll get all three records. So I split the tags and do the search like this:
let a = {name: "1", tags: "foo, bar"};
var finalResult = [];
let tags = a.tags.split(', '); // Get separated tags
for (i=0;i<tags.length;i++) { // Iterate through every tag
// Find matching results in Collection A
CollectionA.find({tags: {$regex : ".*" + tags[i] + ".*"}}, (err, AResults) => {
for (step=0;step<archives.length;step++) {
finalResult.push(AResults[step]);
};
// Find matching results in Collection B after A is done
CollectionB.find({tags: {$regex : ".*" + tags[i] + ".*"}}, (err, BResults) => {
for (step=0;step<archives.length;step++) {
finalResult.push(BResults[step]);
};
res.send(finalResult);
};
};
}
Actually the search goes right and got me the result, but it sends twice because of the loop. Also the app crashes with error:
Error: Can't set headers after they are sent.
So I wanted to solve the problem by only add a condition of "i = tags.length" before res.send, but it doesn't work, and when I console log "i", it just gives me two lines of "2", which is odd because it should have reached that until after the iteration is done. I think it has something to do with the asynchronous-ness of MongoDB commands but have absolutely no idea how to solve the problem. Any suggestions? Many thanks in advance.