0

I am making a web service that serves the data in JSON format. It does so by taking some filters in the form of an array, these filters are then parsed and then checked if any of the values from filter exists in a document's specific field (The field is also an array), it returns all such documents.

Here, filter is the query array. typeFilters is the array in every document. I am using $project to implement it.

This is my code:

function returnAllByFilter(filter, callback) {
    ModuleDBService.aggregate([{
            $project: {
                _id:1,
                title:1,
                typeFilters:1,
                filteredResults: {
                    $let: {
                        vars: {
                            filter: [0,2],
                        },
                        in: {
                            $setIntersection: ["$$filter", "$typeFilters"]
                        }
                    }
                }
            }
        },
        {
            $match: {
                filteredResults: {
                    $ne: []
                }
            }
        }
    ], function (err, data) {
        if (err) {
            console.log(err)
            callback(null);
        } else {
            callback(data);
        }
    })
}

This code works fine, but it also projects the field filteredResults.

Sample of output:

[
    {
        "_id": "3133343230313834754e5871",
        "title":"Physics 202:1",
        "typeFilters": [
            0,
            1,
            3
        ],
        "filteredResults": [
            1
        ]
    }
]

I tried to remove the field from the query and replaced it with the contents inside it directly, "I got errors saying "unknown top level operator: $let". How can I prevent this query from projecting the filteredResults column as well?

Also, is there a better practice to implement this functionality?

metamemelord
  • 500
  • 1
  • 7
  • 19
  • 1
    What exactly are you expecting? You are in fact **telling MongoDB** to explicitly "project" a **new field** of your own naming, yet you seem to be saying you actually don't want it there? Is all you are really trying to do here is **match the documents that contain** either the `0` or `2` value within the `"typeFilters"` field? – Neil Lunn Apr 24 '18 at 10:07
  • @NeilLunn Yes, exactly. I do not want Mongo to project that field at all, I just want it to filter the results based on setIntersection. I read how $project works, after that I removed the field filteredResuts and replaced it with the contents, I got errors. I had to come back to this query. – metamemelord Apr 24 '18 at 10:13
  • 1
    You read the wrong thing then. Because all you really want is `ModuleDBService.find({ "typeFilters": { "$in": [0,2] } })` – Neil Lunn Apr 24 '18 at 10:14
  • It worked, thanks for the help. Also, thanks for also marking it as dupe and redirecting to where the questions is answered, it was helpful as well. Thanks a lot! :D – metamemelord Apr 24 '18 at 10:20

0 Answers0