0

I'm working with mongodb aggregations using mongoose and a I'm doubt what am I doing wrong in my application.

Here is my document:

{
    "_id": "5bf6fe505ca52c2088c39a45",
    "loc": {
        "type": "Point",
        "coordinates": [
            -43.......,
            -19......
        ]
    },
    "name": "......",
    "friendlyName": "....",
    "responsibleName": "....",
    "countryIdentification": "0000000000",
    "categories": [
        "5bf43af0f9b41a21e03ef1f9"
    ]
    "created_at": "2018-11-22T19:06:56.912Z",
    "__v": 0
}

At the context of my application I need to search documents by GeoJSON, and I execute this search using geoNear. Ok it works fine! But moreover I need to "match" or "filter" specific "categories" in the document. I think it's possible using $match but certainly I'm doing the things wrong. Here is the code:

CompanyModel.aggregate(
                [
                    {
                        "$geoNear": {
                            "near": {
                                "type": "Point",
                                "coordinates": [pageOptions.loc.lng, pageOptions.loc.lat]
                            },
                            "distanceField": "distance",
                            "spherical": true,
                            "maxDistance": pageOptions.distance
                        }
                    },
                    {
                        "$match": {
                            categories: { "$in": [pageOptions.category] }
                        }
                    }
                ]
            ).then(data => {

                resolve({ statusCode: 200, data: data });

            }).catch(err => {
                console.log(err);
                reject({ statusCode: 500, error: "Error getting documents", err: err });
            })

pageOptions:

var pageOptions = {
            loc: {
                lat: parseFloat(req.query.lat),
                lng: parseFloat(req.query.lng)
            },
            distance: parseInt(req.query.distance) || 10000,
            category: req.params.category || ""
        }

If I remove $match I get all the documents by location, but I need to filter specific categories... I don't believe that I need to filter it manually, I believe it can be possible with aggregation functions...

So anyone can help me with this mongoose implementation?

Thanks for all help

9
  • Could you show sample pageOptions.category ? Commented Nov 26, 2018 at 21:34
  • Yes. I Put in the question right now. Commented Nov 26, 2018 at 21:36
  • But I mean sample value like is it 5bf43af0f9b41a21e03ef1f9 or ObjectId("5bf43af0f9b41a21e03ef1f9") ? Commented Nov 26, 2018 at 21:36
  • Sorry, I wrote wrong the question. Categories is an array of Strings. Commented Nov 26, 2018 at 21:38
  • 1
    @mickl thank you for your help, the array is of ObjectId and not of String. I'm so dumb... Could you answer the question for me to tag as the best answer? Commented Nov 26, 2018 at 21:59

1 Answer 1

2

In MongoDB you need to make sure that data type in your document matches the type in your query. In this case you have a string stored in the database and you're trying to use ObjectId to build the $match stage. To fix that you can use valueOf() operator on pageOptions.category, try:

{
    "$match": {
        categories: { "$in": [pageOptions.category.valueOf()] }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.