How to use DocumentReference in firestore query - flutter

I have a Firestore Database with two collections, one is called "shops" and the other is called "groups".
In the "shops" collection, there are some documents that represent different shops in a city and in the "groups" collection there is a document with a single field that is an array of reference.
In the array, I saved the last 20 shops that were inserted in the "shops" collection.
I don't know how to get the documents by the reference save in the array.
My method needs to return a Stream<List<Shop>>, I need an idea to resolve my problem, I don't want to use a field in shops' document to save the time when it was inserted.

Related

child referencing in an array VS searching through the children collection

I was wondering if there is any performance difference between having to populate records in a document which are stored as:
createdShapes[
ObjectId('AAA'),
ObjectId('AAA'),
ObjectId('AAA'),
ObjectId('AAA')
]
and between searching through the whole collection as:
CreatedShapes.findMany({createdBy: user._id})
why would I need to store Ids in an array if I can just search using findMany()?

Firestore how to query documents chronologically

I understand that unlike Firebase Realtime DB, there isn't a built in method for querying Firestore documents chronologically.
Other than adding a timestamp field to each document, is there another elegant way to achieve that?
Apparently it isn't recommended to name documents in an ascending names (Do not use monotonically increasing document IDs)

Is there any possible do upsert functionality in "array of an object" using firestore query?

Example :[{
inst:"EVA",
std:"12th"
},
{
inst:"KSF",
std:"12th"
}]
As per the above example, In my case if "inst: "EVA" is already there in the "qualification" array so we need to update the object from the existing one.
Then "inst: "KSF" does not already exist in the "qualification" array so we need to add that one.
Help me if there is any way to upsert using firestore query.
There is no "upsert" operation for objects in arrays. If you need to make changes to that array, you will have to read the document, modify the contents of the array in memory, then update the document with the new contents of the array.
Arrays of objects usually do not work the way that people want, given their limitations on querying and updating. It's usually better to store data as documents in a nested subcollection, so they can be more easily queried and updated by the contents of their fields.

Unable to remove object id from a sub-document made up from array of object ids

I have two collections "notes" and "users". In users collection, I have stored array of object ids of notes that are created by a specific user. I just want to figure out a way such that, when a document from "notes" collection is removed, at the same time the object id of that particular note stored in "users" collection will also get removed.
I am using express and mongoose. I have attached images of these two collections.Notes collection
Users collection

In MongoDB, do document _id's need to be unique across a collection or the entire DB?

I'm building a database with several collections. I have unique strings that I plan on using for all the documents in the main collection. Documents in other collections will reference documents in the main collection, which means I'll have to save said id's in the other collections. However, if _id's only need to be unique across a collection and not across an entire database, then I would just make the _id's in the other collections also use the aforementioned unique strings.
Also, I assume that in order to set my own _id's, all I have to do is have an "_id":"unique_string" property as part of the document that I insert, correct? I wouldn't need to convert the "unique_string" into another format, right?
Also, hypothetically speaking, would I be able to have a variable save the string "_id" and use that instead? Just to be clear, something as follows: var id = "_id" and then later on in the code (during an insert or a query for example) have id:"unique_string".
Best, and thanks,Sami
_ids have to be unique in a collection. You can quickly verify this by inserting two documents with the same _id in two different collections.
Your other assumptions are correct, just try them and see whether they work (they will). The proof of the pudding is in the eating.
Note: use _id directly, var id = "_id" just compilcates the code.