1

I'm trying to obtain the objects inside the reviews array by matching the fb_id's that I'm passing. I do not seem to get them to work. This is my actual document in the DB

{
    "_id": {
        "$oid": "5841d18187dcc74805887a6a"
    },
    "product_id": "583ae172231f5ec01db5727e",
    "category": "Smartphones",
    "reviews": [
        {
            "fb_id": "111",
            "name": "name1",
            "user_img": "imgurl1",
            "title": "asdakj",
            "description": "jnkjnkj",
            "rating": 5,
            "_id": {
                "$oid": "5841d18187dcc74805887a6b"
            }
        },
        {
            "fb_id": "222",
            "name": "name2",
            "user_img": "imgurl2",
            "title": "dadad",
            "description": "asdasdad",
            "rating": 3,
            "_id": {
                "$oid": "5841d1d0cbdf333411530ebd"
            }
        },
        {
            "fb_id": "333",
            "name": "name3",
            "user_img": "img url3",
            "title": "asdad",
            "description": "asdads",
            "rating": 5,
            "_id": {
                "$oid": "5841d4c2174270f807084721"
            }
        },
        {
            "fb_id": "444",
            "name": "name4",
            "user_img": "imgurl4",
            "title": "adasd",
            "description": "kjnkj",
            "rating": 1,
            "_id": {
                "$oid": "5841d569eae1b6600ea1ca92"
            }
        }
    ],
    "__v": 0
}

I will be sending an array of fb_id's to mongoose and want the results only for the objects inside the array that has the fb_id's.

This is my query now:

Review.find({ product_id: '583ae172231f5ec01db5727e' }, { 'reviews.$.fb_id': { $in: <array of fb_id> } }, function(err, results) {
    if (err) {
        throw err;
    };
    console.log(results)

});

But it gives me this error :

MongoError: Unsupported projection option: reviews.$.fb_id: { $in: [ 111.0 ] }

EDIT: After a small query, now I am to get some results. But the result has only the first object that match from the fb_id's and not all.

Please tell me how I can achieve this.

1
  • Please take a look at this one. stackoverflow.com/a/40932617/2683814. Basically you to use $filter aggregator operator to return all matching records for a given criteria. Commented Dec 3, 2016 at 20:15

1 Answer 1

1

You can use this for reference. your code would be something like this:

Review.find({
    product_id: '583ae172231f5ec01db5727e'
    },
    {
        reviews: {
            $elemMatch: {
                fb_id: {
                    $in: <array of fb_id>
                }
            }
        }
    },
    {
        _id: 0,
        'reviews.$': 1
    }
, function(err, results) {
    if (err) {
        throw err;
    };
    console.log(results)
});
Sign up to request clarification or add additional context in comments.

3 Comments

this returns only the first item that matches from the array. It does not return all the items that match
maybe its because of product_id! it's supposed to be unique?!
Yes product ID is unique. And I want the reviews matching fb Id's only for that product ID.

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.