SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

How to use aggrigation..? #73

Closed singhvikram704 closed 8 years ago

singhvikram704 commented 8 years ago

I have collection:

{ "_id" : ObjectId("56696db1429cbd907085cb08"), "attributes" : [ { "_id" : "5666cec9e4b0834319eac761", "name" : "Quantity", "value" : "1kg" }, "status" : "Active", "type" : "single" }

How i use aggregation in restheart like query:

db.products.aggregate({$group : { _id : "$Quantity"}})

Need help regarding this...

Thanx in advance

ujibang commented 8 years ago

Refer to Aggregation documentation section

In your case, you need to add the following metadata property to the collection products and you can then get the aggregated data at URI /db/products/_aggrs/myaggr (not the uri property)

"aggrs": [{
            "type": "pipeline",
            "uri": "myaggr",
            "stages": [ {"_$group": {"_id": "$Quantity"} } ]
        }]
hemraj-sharma commented 8 years ago

Hi, I can use aggregation through restheart successfully. Thank You very much for Your support. But in a particular case, when I have to use Match for inside the document or after the unwind. I am getting invalid BSON field name. For example: ------->>>>>>> [ {"$unwind": "$abc_Ids"},{ "$match" : { "abc_Ids.abcId" : ""}},...] <<<<<<<<--------- "works fine on the mongo shell." as will be used as======>

[ {"_$unwind": "$abcIds"},{ "$match" : { "abcIds.abcId" : {"$var": ""} }},...] in the aggrs variable.

I am getting error as ------------>>>>>>>>>>>>>>> "http status code": 500, "http status description": "Internal Server Error", "message": "Error handling the request", "_embedded": { "rh:exception": [ { "exception": "java.lang.IllegalArgumentException", "exception message": "Invalid BSON field name abc_Ids.abcId", "stack trace": [ "java.lang.IllegalArgumentException: Invalid BSON field name abc_Ids.abcId", " at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:494)", <<<<<<<<<<<<<<<<<<<<<<<---------------------------------------------- Can you guide in this regard. Thanks. Regards, hemraj

hemraj-sharma commented 8 years ago

editted above content: ---------->>>>>>>>>> as will be used as======>

[ {"_$unwind": "$abcIds"},{ "$match" : { "abcIds.abcId" : {"$var": ""} }},...] in the aggrs variable. <<<<<<<------------

ujibang commented 8 years ago

as you can read in the documentation the aggregation operator must be prefixed with _

this is because in mongodb fields names cannot start with _

for example, to use $unwind you have to actually pass _$unwind in the aggregation definition

hemraj-sharma commented 8 years ago

I am already using in that way. For the same I again edited the query. But somehow they are being converted to the present view. I am using $match as $match and "$unwind" as $unwind. though I am getting the problem which I have asked for.

ujibang commented 8 years ago

Can you pease past the full json you used to create the aggregation?

Just in case since I had this issue in the past: are you using a command line tool in a terminal where $xxx is interpreted as an environment variable?

In case escape it \$xxx