mongodb / stitch-js-sdk

MongoDB Stitch JavaScript SDK
Apache License 2.0
113 stars 67 forks source link

$facet not working #435

Open nalnir opened 3 years ago

nalnir commented 3 years ago

I'm getting this error trying to use $facet in aggregation pipeline:

{message: "aggregation stage "$facet" is not supported", name: "e", errorCode: 5, errorCodeName: "InvalidParameter", stack: "e: aggregation stage "$facet" is not supported↵   …m/stitch-sdks/js/bundles/4.8.0/stitch.js:1:203137"}

Are you gonna support $facet in the future or is my code incorrect? Im trying to get a count of all documents after I limited them.

stitchApp.auth.loginWithCredential(new AnonymousCredential()).then((user) => {
                db.collection('products').aggregate([
                    { $facet: {
                        "products": [
                            { $match: query },
                            {
                                $group: {
                                    _id: "$groupid",
                                    id: { $last: "$_id" },
                                    images: { $last: "$images" },
                                    description: { $last: "$description" },
                                    barcode: { $min: "$barcode" },
                                    category: { $last: "$category" },
                                    subcategory: { $last: "$subcategory" }, 
                                    discount: { $last: "$discount" },
                                },
                            },
                            { $sort: { '_id': 1 } },
                            { $limit: 30 }, 
                        ],
                        "totalCount": [
                        { $match: query },
                            {
                                $group: {
                                    _id: "$groupid",
                                    count: { $sum: 1 }
                                },
                            },
                        ]
                    } },
                ]).toArray().then(result=>{
                    ...
                })
})
drewdipalma commented 3 years ago

Hi – Unfortunately it's very difficult for $facet to work with our permissions so it's not currently supported by the SDKs. However, you can create a Function which runs as System and that uses $facet. You can find more information on aggregation expressions with limited availability here.

nalnir commented 3 years ago

Alright, will check the link you sent. Thanks