I am currently practicing MongoDB by building a site powered by it. The db has 5 collections and uses cross-references, either through objectid references or through a hybrid objectid-embedded document system. Mostly things have went very smoothly.
Now, I am trying to build an advanced search function. One field lets you insert multiple strings, separated by commas. In the backend, these will be separated into an array. The objects that are being searched all have an array field which normally contains one or more embedded documents. All of these embedded documents have an objectid reference and a "title" property. This query array I mentioned lets users search for multiple of this title entries, and I want to return all objects that contains the embedded documents with "title" properties all matching the array, so if you search for "X,Y" and there are a document with two embedded documents with the title properties "X" respective "Y" they will be returned, whereas if only of those embedded documents is present, that document will be ignored. The document structure looks like this:
{
"_id":"5dfcd95fe9d81c32437ba3fb",
"usedBy":[
{
"_id":"5ddaf35f51528c0881e84507",
"title":"X"
},
{
"_id":"5e00b2c04facc56b40930eac",
"title":"Y"
}
],
//more properties follow...
}
So far however, without success. I have tried a few methods, like $in and $elemMatch, but they don't seem to do what I want to have them do. Which method would you suggest? Right now, I tried to converted the array of strings to an array of objects, which in turn are passed to a filter object for the "find"-method:
if(usedBy){
usedBy = usedBy.split("%5C%2C%2");
for(var i=0; i<usedBy.length; i++){
usedBy[i] = {title: usedBy[i]};
}
filter.usedBy = usedBy;
}
Thanks.