how to move firebase document included all nested data to other collection? - javascript

I want to move a specific doc with all his nested collections from one collection to another.
is it possible?
db.collection('codes).doc(specificDoc).setLocation(db.collection('archive))
or somthing like this

Firestore does not offer any way to move documents between collections. What you will need to do instead is query for the documents to move, iterate them, write new documents in the archive collection, and delete the original.
Consider instead just using a field in each document to mark whether or not it is archived, and use that field to filter the results.

Related

Relations with mongoose and mongodb

I have been looking for a way to relate a collection that has an array of objects, each object belongs to a different user, I would not like to force my backend by making it extract manually through maps or reduce the ids that belong to each user, I would like to know if is there a way to use populate so that only matching ids are added to my list
So far I have not been able to find information about it, I hope someone can help me

Referencing firebase document variables from another document

I'd like to have two separate database collections (users and public-users). To access/edit documents in the users collection, you need to be the owner of the document. However, for a public leaderboard, I want the displayName and uid of each user in the users collection to be available for everyone to read. To save me the hassle/chance of error, I'd like to reference the documents displayName property (from the users collection ) in the public-users collection.
Is something like this possible?
Firestore does not have any way to link the value of a field in a document to another specific field in other document. You have two options:
Duplicate the entire value between the fields in both users and public-users. You will likely need to keep all the copies up to date if you want any one of them to change.
Write client code to read the document from public-users that matches the document from users. This requires two read operations. There needs to be enough information in the public-users document to find the matching users document. It sounds like they could simply have the same document ID.
There is no way to make it any more simple than these options.

Why I have no results with a collection group query?

I'm using for first time collection groups querys on firestore in my web application but I can't retrive any data and I don't know why
I'm using Firebase SDK 7.6.1 and I have created the indexes correctly i think.
This is my Collection data structure:
And this are my Indexes:
And finally the code with I'm trying to retrieving data is:
doc = this.database.collectionGroup('Authors')
.where('Name','==',this.TextParams.trim().toLowerCase());
I expected to retrieve the documents where the author name is equal to my TextParams variable
In your screenshot, Authors is not a subcollection, it's an array type field. Since it's not a subcollection, a collection group query won't work at all. Also, it's currently not possible to search among array field items.
What you will have to do is actually put your Authors into a subcollection.

How can I order the documents in Firestore?

How can I order the documents in Firestore? It looks like Firestore lists documents in alphabetic order by the ID I let Firestore automatically create. But I don't want that. I just want to see my newly added document added as the last document in the collection. How do I do that? Should I create my own alphabetic ID's?
Example of my collection in Firestore:
Firestore is meant to scale massively. At that scale, it doesn't matter what order the documents appear in the console, because the console is useless for viewing massive numbers of documents within a collection.
If you still need an ordering in the console, you will have to come up with document IDs of your own.
If you have a timestamp field, you can add a filter on timestamp and sort results by desc. Do not add a filter condition.

Trouble updating Mongodb properly

plnkr
I am trying to traverse through a collection, and update each document respectively.
My UserProfile collection consists of multiple JSON objects of userProfiles. As you can see, each profile has a lot of the same information. The only difference is the personal information. (This is just a test case of hard coded objects. The real data will be in an SQL DB managed by a sysadmin).
What I am trying to do is write a function (replaceTopics) that will take in an array of topics and replace each topic that matches in the collection. So if the system admin makes a change to a topic/s, he will send me the topic/s and I will be checking each document in my userProfile collection to see if that document has the matching topic (by matching topicIDs), if so, I need to replace that entire topic with the editedTopic.
I have tried this but with no luck. You can take a look at my function.

Categories