phplaravelmongodb-queryxapilearning-locker

How to write gte filter for Learning Locker Aggregation API (Arguments must be aggregate pipeline operators)


I'm writing a request for LRS service. I need to get from there statements that were created in the range of some date. The filters gte and lte are suitable for this. But the documentation (and this) does not say how exactly they should be written. Should I use and or write them inside the field:

"statement.timestamp": {
    "$gte": "2022-04-01T00:00:00Z", 
    "$lte": "2022-05-01T00:00:00:00Z"
}

I'm also getting confused with the parentheses. Some examples have {}, others have [{}], others just [].

I understand that this is a Mongo specificity.

Speaking of Mongo. In its documentation there is an example of Syntax: { field: { $gte: value } } }, but I catch an error with it (Arguments must be aggregate pipeline operators).

I did a test with this query: pipeline=[{"$limit": 1}]. Everything worked fine for me.

Now my query looks like this:

pipeline=[{
    "$match": {
        "statement.timestamp": {
            "$gte": "2022-04-01T00:00:00:00Z"
        },
    }, "$limit": 1
}]

And with it I catch an error (Arguments must be aggregate pipeline operators).

I tried different combinations of brackets {} and [], but I think my error lies in the query construction.

Thanks !


Solution

  • The ray from the comment helped a lot with finding a solution.

    Quote:

    You could be comparing to string values to a date field. Make sure you are using datatypes consistently. Also, $limit should be an individual stage. See if this helps with your case – ray

    $limit really needs to be put into a separate staging. Specifically in my case the 502 error started to be thrown, and it was due to the fact that there were no records in the database satisfying the filter. (Or some other reason). Nevertheless, the code worked successfully with this variant of the query and returned several records to me.

    'pipeline=[{ "$match": { "statement.timestamp": { "$gte": "2020-11-30T15:21:52.698Z", "$lte": "2020-12-30T15:21:52.698Z" } } }, {"$limit": 5}]'

    Thanks !