How can i generate custom IDs for objects in firebase? - firebase

Please I want to give custom IDs to my database objects in Firebase but I don't now how to do it. Firebase creates default IDs for database objects which I don't want. I want to be able to assign my own IDs to objects or the child nodes of in the database for unique identification.

Most likely you're adding the items to the database with something like:
ref.push().set("my value");
This generates a new unique key under ref and sets your value on it.
If you want to use you own key/name for the child location, add the item with:
ref.child("my key").set("my value");

You cannot customize ID of firebase object, but you can create another field with ID role.
ref.child("my_id").set("customize_id");
after that, using "Filter by key" to get exactly your object you want.
In our case: We need to have a user_id type Int and auto-increase, so we can't use default _id of firebase object, we create user_id ourself to solve this problem.

Related

Cosmos DB simple ID field for end user

Is it possible to include a user friendly ID field into cosmos db documents? This doesn't need to override the default id field that generates when adding a document but can be a custom one that is simple for an end user to know and search for.
Example document, the ref field is what I want to generate as a simple human readable identifier.
{
"id": "57275754475457-5445444-44420478",
"ref": "45H7GI",
"userId": "48412",
"whenCreated": "D2021-11-09T21:56:31.630",
"tenantId": "5566HH"
}
I'm looking at building a ticketing system and would like a simple ID field for a user to be sent and who can reference when updating/ searching for.
Any help with this would be appreciated.
For your own purposes, you can choose to either use id (which is guaranteed to be unique within a partition) or your own property (such as ref as you defined in your example). For any property other than id, you'd need to add a unique-key constraint when creating the container (and at that point, ref would be unique within any partition, just like id).
Really your choice whether you store your custom id's in id or ref. Just know that, if you ever want to do a direct-read (instead of a query), you can only do a direct-read against an id, not against any other property.

Sub-field in firestore with react native

I have a collection "Tasks" that include some fields : The user that added the task, the name of the task, the category and I want to add another one with the steps of the task. Do I have an option to add a "Steps" field (and not collection!), and in this field I will have all the steps of the task?
Here the image of my firestore structure : Firestore structure
Of course, just use update
collection('Tasks').doc(docId).update({"steps" : "yourSteps"})
yourSteps here can be a string, int, list, a whole new map, anything. Without affecting the other fields in your document like category.
Important note:
.set() can also merge new data into the existing document if you pass the merge options:
.set(data, {merge: true})
.set() will write to the document if it exists, and will create a new document if it does NOT exist - with the mergeOption, it can merge in new fields or update selected ones.
.update() is similar, BUT if the document does not exist .update() will FAIL..
https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#set

How to handle custom user ordering in Firestore

From the context of a todo application, the user has a list of todos, if they reorder an item anywhere in the list how could that be saved in Firebase Firestore?
I currently have a collection with ALL todos. They get filtered by user ID and day, but I’d like to allow for custom ordering of todos. How could that be achieved?
In order to maintain the previous costs while also allowing todo items to be ordered per user you can utilize a new field per document in the todo collection. You can attempt to add a field, named lets say "order", which will hold the numerical value for the order of the todo element for each user. For example "0" for first position,"1" for second, and so on so forth. These will then be filtered by user ID and day as mentioned previously.
There is no inbuilt solution available for this. The solution I will suggest is to keep a separate document per user which maintains the order of documents (in this case to do items) in an array and when you show the data to the user in the UI, use the document to order the items on the page.
But keep in mind this approach will increase your database costs because you will need to perform an update to 2 documents whenever a new todo is created.

How do i add children to Firebase player UID?

I tried to add children nodes to a player with UID. I used child("node_1"). It creates only one node. How do I add items to this node?
To solve this, you need to have a unique identifier every time you try to add data to your database. Because a Firebase database is a NoSQL database and is structured as pairs of key and values, every node is a Map, which means in the case of Map, it replaces the old value with the new one.
Fortunately Firebase provides a method named push(). You can use it like this:
yourRef.push().child(name).setValue("John");
Which one you use add child node? Web, Ios or Android?
Alex Mamo was right: "you need to have a unique identifier every time you try to add data to your database"
Using for Web:
let node = firebase.database().ref().child(Your node)
var key= node.push().key // Get a key for a new
node.child(key).set(new value)
https://firebase.google.com/docs/database/web/read-and-write?authuser=0
Using for IOS:
let key = ref.child("posts").childByAutoId().key
https://firebase.google.com/docs/database/ios/read-and-write?authuser=0
And for Android:
String key = mDatabase.child("posts").push().getKey()
https://firebase.google.com/docs/database/android/read-and-write?authuser=0
Hope to help you.

When to go for hashmap and arraylist?

Please explain above question with example scenario I am confusing which is best.
If you to fetch a specific object based on keyword or any identity in list then you have to iterate the list get object and compare with its values
In map you can directly create key value pair..you can pass key and get the value.
ex:
A object user is present which has several properties one of them is user code
Now if you have list of user object then you will fetch one by one user object and compare the code of each user...but in map you can directly store user object with user code as key pass the key and get the desired object
map.get("key");
but if you requirement is not based on key type access better to use list.. example as you to just display list of items or you have to perform sublisting.
Too broad question, but will try to shorten it:
When you have to get the value based on key (key can be anything) then you go for hashmap. Consider a telephone directory where you go to appropriate name and search for person's name to find his number.
While if you have similar object's and want to store them somehow and later on retrieve it say by index or traverse them one by one then you go for list. So if your task is to find employees older than age 50 yrs, you can just return a list of employees who are older than 50.

Resources