Firebase Unique Value

Is it possible to get an array of unique values in firebase?

Nope, you cannot query a collection for unique values of a specific property of each item. You can filter on such values, so ref.child('beers').orderByChild('type').equalTo('IPA'). But that's not what you were asking. :-)

Typically if you want this type of operation in Firebase (or most other NoSQL databases), you'll keep the items (or references to the items) under a group:

{
"beer_types": {
"IPA": {
"-jwhkclclmecl": {
"name": "Two Hearted Ale",
"brewery": "Bells"
},
"-hcwiu3cp902d": {
"name": "Diabolical",
"brewery": "North Peak"
}
},
"Pale Wheat": {
"-ckqjheh292od": {
"name": "Dirty Blonde Ale",
"brewery": "Atwater"
},
}
}
}

Of course that only works if you want to only keep them in one category. If you want multiple categories, you can store references to each item under multiple categories:

{
"beers": {
"-jwhkclclmecl": {
"name": "Two Hearted Ale",
"type": "IPA",
"brewery": "Bells"
},
"-ckqjheh292od": {
"name": "Dirty Blonde Ale",
"type": "Pale Wheat",
"brewery": "Atwater"
},
"-hcwiu3cp902d": {
"name": "Diabolical",
"type": "IPA",
"brewery": "North Peak"
}
}
"beer_types": {
"IPA": {
"-jwhkclclmecl": true,
"-hcwiu3cp902d": true
},
"Pale Wheat": {
"-ckqjheh292od": true
}
},
"breweries": {
"Bells": {
"-jwhkclclmecl": true
},
"Atwater": {
"-ckqjheh292od": true
}
"North Peak": {
"-hcwiu3cp902d": true
}
}
}

Firebase firestore unique values in multiple fields

What you're considering is pretty much the only way to guarantee uniqueness of a value across the database.

In a few more structured steps, it'd be:

  • Use that value as the document ID in an secondary collection. This collection purely exists to ensure uniqueness of the IDs.
  • Let the user claim it, typically by writing their UID into the document.
  • Use security rules to ensure a user can only write a document if it doesn't exist yet, and (if needed) only deleted when they own it.

The topic of unique values has been covered quite a few times before, although usually in the form of unique user names, so I recommend checking out:

  • Cloud Firestore: Enforcing Unique User Names
  • How to generate and guarantee unique values in firestore collection?
  • How to enforce Uniqueness in a Property of a document field in Google Cloud Firestore
  • Firestore unique index or unique constraint?
  • I want to make unique usernames in firebase/firestore

How to implement unique constraint on a child value of a node in Firebase?

Try the following:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("user_detail").child("1th_role");
ref.orderByChild("roll_no").equalTo(number_entered).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
if(dataSnapshot.exist() {

Toasty.makeText(Activity_Name.this,"Number already exists",Toast.LENGTH_SHORT).show();

}
else{
//add data
}

}
@Override
public void onCancelled(DatabaseError databaseError) {

}
});

Since the student will enter the number, then do a query orderByChild("roll_no").equalTo(number_entered) that checks if the roll number that is entered already exists in the database.

How to generate and guarantee unique values in firestore collection?

The easiest to guarantee that some value is unique in a collection, is to use that value as the key/ID for the documents in that collection. Since keys/IDs are by definition unique in their collection, this implicitly enforces your requirement.

The only built-in way to generate unique IDs is by calling the add() method, which generates a UUID for the new document. If you don't want to use UUIDs to identify your orders, you'll have to roll your own mechanism.

The two most common approaches:

  1. Generate a unique number and check if it's already taken. You'd do this in a transaction of course, to ensure no two instances can claim the same ID.

  2. Keep a global counter (typically in a document at a well-known location) of the latest ID you've handed out, and then read-increment-write that in a transaction to get the ID for any new document. This is typically what other databases do for their built-in auto-ID fields.

Getting the value of a child of a unique ID from Firebase

You are getting the foll9ow error:

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference

Because you are using an incorrect reference in your code. As I see, both "latitude" and "longitude" are direct children of the UID. So please change the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Users");
DatabaseReference uidRef = rootRef.child("User Location");
uidRef.get().addOnCompleteListener(/* ... /*);

To:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("User Location").child(uid);
uidRef.get().addOnCompleteListener(/* ... /*);

The rest of the code may remain unchanged.

How to enforce Uniqueness in a Property of a document field in Google Cloud Firestore

There is no way to enforce unique values of fields in Firestore. The only uniqueness you can guarantee is the document IDs within a collection, so the solution is to create a new collection where the user names are used as document IDs.

This has been discussed a few times before, so I recommend checking out:

  • Firestore security rule to check if character username already exists
  • Check a document field for a specific value in Cloud Firestore
  • I want to make unique usernames in firebase/firestore, which shows some of the relevant security rules.
  • Cloud Firestore: Enforcing Unique User Names
  • Firestore unique index or unique constraint?, a more complete write-up with code and rules samples.
  • Cloud Firestore: Enforcing Unique User Names

unique property in Firebase

There is no way to ensure unique values in the Firebase Database.

But on the other hand keys are always unique within their context.

You can make use of this to model your data in such a way that it guarantees uniqueness of the property you want. Say you want the category name to be unique, store the categories under their name:

-Categories
-name
- color:
- sum:

With this structure you're guaranteed to have unique category names.

If you must store the categories under their current keys, but still want to ensure unique names. You can create a secondary index, which uses the category names as keys to ensure their uniqueness.

-Categories
-key
- color:
- name:
- sum:
-CategoryNames
-name: key

This latter approach is explained further in Kato's answer to Enforcing unique usernames with Firebase simplelogin



Related Topics



Leave a reply



Submit