Sort gcloud firestore operations by start time descending - google-cloud-firestore

I am trying to get the status of the latest firestore export via Gcloud CLI.
However, if I execute
gcloud firestore operations list --limit=5 --sort-by="~metadata.startTime"
the results are not sorted by the start time in descending order.
If I add --format="value(metadata.startTime)", I get the correct output
2023-01-17T00:05:13.818312Z
2023-01-17T15:30:19.362908Z
2023-01-18T08:11:18.433556Z
2023-01-19T00:05:01.440255Z
2023-01-20T00:05:04.135387Z
How can I achieve the results being sorted by the startTime in descending order?

At first glance it seems you are using the --sort-by flag incorrectly it should be used with [ ] as per the documentation.
The final command would be:
gcloud firestore operations list --limit=5 --sort-by=[~metadata.startTime] --format="value(metadata.startTime)"
This will list the last 5 operations and sort them by start time in descending order.
But when I tried with above command also it seems the issue is still there.I have raised this issue at with google.

Related

Spring boot mongo template remove with limit query

I am trying to delete a limited set of mongo documents from a collection which have id less than 10 but want to remove them in sets of 3, so tried using limit, but it still deletes all the documents and ignores limit.
Query query = new Query();
query.addCriteria(Criteria.where("_id").lt(id)).limit(3);
mongoTemplate.remove(query,TestCollection.class);
When I perform mongoTemplate.find(query,TestCollection.class); limit works fine and returns 3 element at a time but in remove it doesn't works.
Is there any other way to delete in single query only.
To achieve this do it in two passes
Find 3 ids to delete as you are doing currently
do a collection.remove with Criteria.where("_id").in[id1,id2,id3]
I would also add a sort criteria before doing a limit. Otherwise the results of deletion might be dependent on the index used

Filter results on the Firestore console by timestamp field

How can I filter the results on the Firestore Console by a timestamp field?
On the collection users, we have a field createdOn of type timestamp. If I want to filter the collection by field, I get the following dialog
I have tried entering the date as string
2019-09-15
2019-09-15T00:00:00Z
I have also tried using a timestamp as number in millis and seconds
1568505600000
1568505600
When looking at the requests sent to Firestore, the structured query uses a field filter with a value of corresponding to either stringValue or integerValue, but I think the timestampValue would be the right thing.
I do not want to adapt the query in the console and build my own requests. I know that there is always the option to sort the documents in the collection and then scroll to where it's interesting, but this will load all documents that are not relevant.
Is there a simple way to do what I want?
There is a new query builder tab in the console (I do not know when this was introduced, but I assume during the Firebase Summit 2022). For the query above, this would look like this
It even has a timestamp type in the select list.

Mongo find all documents from a particular month

I have a collection with documents containing a date.
Is there a way to find all documents from a particular month (all years), using db.myCollection.find()?
I managed to achieve approximately what I need by using the aggregation pipeline and the $month operator, but I couldn't find anything similar among the 'query and projection' operators.
I do not need to group the documents (I only need to filter them).
I am modifying some code that creates filters dynamically based on the user selection and the code uses find with the generated filters.
I would like to be able to give a user the ability to see only entries from particular months (say, only entries for the summer season).
Finally is there a performance difference between using a filter with find and using the equivalent filter in a $match stage with the aggregation pipeline ?

MongoDB - Can I have multiple sort fields in a map reduce?

I have a map/reduce that I noticed was taking nearly 10 seconds to run, even though no results were being returned. Using the mongo shell, I was able to determine that my initial query was the culprit. I was able to add a 2nd sort field to the query, which sped it up drastically.
However, when I try to add that 2nd sort field to my map reduce, I get the error "could not create cursor over [collection] for query ...". Is there any way for me to add a 2nd sort field to the map reduce?
Edit: The goal of my query is to find the first record created by each user/day. So the key that I am emitting is the user's id + created on day, ignoring any time. That way I am grouping all records created by a user on a given day. In my reduce, I am then taking the record that was created first. I have actually ditched using map/reduce and am now doing essentially the same thing, but with a normal find() and then some javascript to group and reduce.

pymongo sort grouped results

I need to group and sort by date_published some documents stored on mongodb using pymongo.
the group part went just fine :) but when I'm addding .sort() to the query it keeps failing no matter what I tried :(
here is my query:
db.activities.group(keyf_code,cond,{},reduce_code)
I want to sort by a field called "published" (timestamp)
tried to do
db.activities.group(keyf_code,cond,{},reduce_code).sort({"published": -1})
and many more variations without any success
ideas anyone?
You can't currently do sort with group in MongoDB. You can use MapReduce instead which does support a sort option. There is also an enhancement request to support group with sort here.
Although MongoDB doesn't do what you want, you can always use Python to do the sorting:
result = db.activities.group(keyf_code,cond,{},reduce_code)
result = sorted(result, key=itemgetter("published"), reverse=True)