MongoDb get position of an item in the collection with sorting - mongodb

I use a table with sorting and pagination.
To get data I use find with sort, skip, limit, to get data for the current page of the table.
Question: When I add a new item to the collection, I need to open the page in the table.
That is, I need to get the position of the item in the collection, taking into account the sorting.

Related

custom query to choose " which collection to query from" in flutterflow

At the moment you can only query a single collection in firebase on a container/listview/widget etc.it would be good to be able to change which collection is being queried with an on click action.
In the dashboard, on-clicking the button the listview should change the collection(first) it is being queried from to second collection and display data (of second collection)
Not able to write custom function to pick which collection to be queried

Mongodb new data insert in top

Usually mongodb saves previous data on top. Can we save data as new at the top?
Mean i want to insert my new data on 0 no index every time and pervious data will go down as Array.unshift() method...
Not sure if I got you right.
Within a document:
This could be helpful $position
Within a collection:
You could just sort by _id descending when accessing the data to get new documents first.

Is there a method in Firestore that could fetch a document and its adjacent documents?

In Firestore, we need to use something like startAt or endAt with limit to get either previous or preceding data from a given point in the list of documents within a collection.
We need to fetch twice to get adjacent documents (once for the data before and once for the data after) then we need to combine them in the list which could be error prone
I was wondering if there is a way to get the corresponding document with its adjacent documents in Flutter Firestore? Something like .getDocumentWithAdjacent
Firestore.instance.collection('restaurants').document(docId)
.getDocumentWithAdjacent(previousCount: 3, afterCount: 6); // will fetch 10 documents
The command above will fetch 10 documents: 3 previous documents, the document itself, and 6 preceding documents.
Just further explanation & use cases:
Just like Instagram app for example, the user can tap on a post to see its detail. Then the user can scroll up and down to see the adjacent post details easily because when the user tap on a post, some of the data before and after that post also being loaded.
Another example: A restaurant search app that shows a list of restaurant in an area.
When a user tap somewhere in the middle of the list of restaurant, then the restaurant details is being displayed, but the user want to scroll (either horizontally or vertically) to see the adjacent restaurant in the list so we need to load the data for that also.
Thank you
Firestore does not have a concept of "adjacent" documents in the way that you describe.
What you can do instead is make two queries. One for documents greater than a given document, then another for document less than the given.

Sort documents in firestore collection chronologically

Is there a way to sort documents in collection chronologically when they are created? Currently, they are all over the place. For example, in To-Do app, when you add new item to collection, it should display at the bottom, last, not somewhere in the middle.
You will need to define an order based on some data in the document, and order your queries based on that field.
The typical solution for time-base order to make sure your documents all contain a timestamp field that you can use to sort them. When you call add() (or other methods to update data), you can tell Firestore to use the current time using FieldValue.serverTimestamp():
collection(...).add({
..., // your other fields
createdOn: FieldValue.serverTimestamp()
})
Then you can use that field to sort when querying with orderBy():
collection(...).orderBy('createdOn')
Try using DateTime.now() for the document ID. This should put the collection in chronological order.
For example:
Firestore.instance.collection('Posts').document(DateTime.now().toString()).setData({});

MongoDB: Sort if value is present in array or not

question: I have a document where a field picked_by_ids is an array of all user ids. Now I have to sort this table such that if provided id is there in picked_by_ids then it should come at top and all other records should list after that.
Note: We may have a large table and need to add indexing for this. Also need to add pagination.
Thanks.