I am using firebase realtime database to track the inventory on a track. I am doing something simple. As you can see in the image above an item has been added twice to the database instead of adding the quantities. How can I check that an item with a certain SKU and location_name already exists in the database?
If you want to store the inventory per product, and products have unique keys already, consider using that existing key in the database, instead of using push() to generate a new unique ID.
So where you now do something like:
db.push().setValue(objectForSkuPVC005PBPSTA);
Instead do:
db.child("PVC005PBPSTA").setValue(objectForSkuPVC005PBPSTA);
That way you can always update the child at db.child("PVC005PBPSTA") when you need to change its quantity.
A second unrelated point: you're storing the quantity as a string. I recommend storing it as an actual number, so that you can use numerical operations (such as the new ServerValue.increment()) to manipulate it.
Related
This question already has an answer here:
Firebase query if child of child contains a value
(1 answer)
Closed 3 years ago.
I need a firebase query to filter the list based on the value of an array.
if any of the index of GID(Array) contains the given key. e.g my key is YsGMyfLSGRNHxDWQmhpuPRqtxlq1 and one node's GID have that on 0th index and other have that on 1st index. So these two lists need to be returned.
Currently, I can only get the one at 0th index using the code
//userID = YsGMyfLSGRNHxDWQmhpuPRqtxlq1
firebaseDatabase.child("Groups").queryOrdered(byChild: "GID/0").queryEqual(toValue:userID)
When I try to combine the query I am getting errors.
I don't know about your database structure, But I can explain that There is a limitation in Firebase Realtime database that you can only order by 1 child.
So now if we require to order by 2 Childs we can combine 2 nodes and make it 1 node and can apply order by query on it. For example
If we have username & email fields we can make a new field username_email and can apply order by on it.
Like
user: {
username: "john",
email: "john#g.com"
username_email = "john_john#g.com"
}
Now we can write
firebaseDatabase.child("user").queryOrdered(byChild: "username_email").queryEqual(toValue: "john_john#g.com");
There is no way you can filter your groups based on a value that exist within an array. If you want to query your database to get all groups a particular user is apart of, then you should consider augmenting your data structure to allow a reverse lookup. This means that you should add under each user object the groups in which that user is present.
This means that you'll need to duplicate some data, but this is not a problem when it comes to Firebase. This is a quite common practice, which is named denormalization and for that, I recommend you see this video, Denormalization is normal with the Firebase Database.
When you are duplicating data, there is one thing that need to keep in mind. In the same way you are adding data, you need to maintain it. With other words, if you want to update/detele an item, you need to do it in every place that it exists.
However, what you need is actually allowed in Cloud Firestore. Its array-contains operator allow you to filter documents that have a certain value in an array. For more on this topic, please see the following post:
Better Arrays in Cloud Firestore.
In my project i want to fetch all records from table with sort by created_date descending order.
Also i want to add condition of fetch all item not created_by login user.
I have tried many ways but not able to achieve it.
Below is my table structure.
Here is my java code to fetch records from dynamoDB.
Map<String, Condition> filter = new HashMap<String, Condition>();
//filter.put(RealmConstant.Expo.created_by, new Condition().withComparisonOperator(ComparisonOperator.NE).withAttributeValueList(new AttributeValue().withS(userId)));
filter.put(RealmConstant.Expo.created_date, new Condition().withComparisonOperator(ComparisonOperator.LE.toString()).withAttributeValueList(new AttributeValue().withS(""+new Date())));
Expo expo =new Expo();
expo.setCreated_by(userId);
DynamoDBQueryExpression<Expo> queryExpression = new DynamoDBQueryExpression<Expo>();
queryExpression.setHashKeyValues(expo);
queryExpression.setIndexName(AppConstant.DynamoDBTableIndex.created_by_created_date_index);
queryExpression.setConsistentRead(false);
queryExpression.setRangeKeyConditions(filter);
queryExpression.setScanIndexForward(false);
return mapper.query(Expo.class, queryExpression);
As per above code i am getting all records created by me only. I want to fetch all records not created by me.
Also tried .withFilterExpression("created_by <> :val1").withExpressionAttributeValues(eav); but not working. As already question posted. Why is there no **not equal** comparison in DynamoDB queries?
and
DynamoDB: Filter Expression can only contain non-primary key attributes
The short answer is that you can’t fetch *all the items from a DynamoDB table in sorted order, by any attribute. DynamoDB just doesn’t work that way.
Think of DynamoDB as a distributed hash map of lists.
Just the same as you can’t expect to be able to get globally sorted results from such a map of lists, you can’t get them from DynamoDB either.
You can scan the whole table, and even filter, out some unwanted results as you go, but for sorting, you need to do it after you’ve fetched the records.
What you can do is retrieve items that have the same partition key, in order or the sort key.
And you can create an index where you pick an arbitrary attribute as the partition key and another as the sort key but even that approach has some limitations.
The best way to go is to really take some time and think about what you are going to do with the data. Why are trying to retrieve all items from the table in sorted order? Perhaps there is a better way to organize your data such the you din’t need to retrieve all of it.
I currently have an app where I store user data in a SQLite database, and one of my fields is a User ID. I would like to add an option to auto-generate User IDs in an mmddyyXXX format, where XXX is a sequential number per user that resets every day.
Does anyone know how I would approach this? I looked at some of the other similar questions, but they don't seem to be helpful.
This is not complicated at all. If your'e similar with SQLite in android just take the date and the userId using a SELECT and generate that string yourself.
If the XXX is not the userId just save another table containing 'tokens' for users. every userId would have a 'token'.
Every new day just change the contents of this table.
I believe you could use a TRIGGER that will generate the userid when a row is inserted.
The following may suit :-
CREATE TRIGGER IF NOT EXISTS newuserid AFTER INSERT ON users
BEGIN
UPDATE users SET userid = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)||
(
SELECT CAST(substr('000',1,3-length(count()+1)) AS TEXT)||CAST((count()+1) AS TEXT)
FROM USERS
WHERE substr(userid,1,6) = strftime('%m%d',date('now'))||substr(strftime('%Y',date('now')),3)
)
WHERE userid IS NULL;
END;
The trigger is named newuserid
userid is the column for the auto-generated id. The above relies upon it being NULL so it cannot be a PRIMARY INDEX.
There is no reliance upon other columns.
Testing
Starting with an empty table :-
Inserting 4 rows using INSERT INTO users VALUES(null,'This is a new user'); results in :-
To check for another date the rows are adjusted from 041018??? to 040918??? as per :-
4 more rows are inserted using INSERT INTO users VALUES(null,'This is a new user');, resulting in :-
Note this answer isn't intended to be fail-safe but rather the basis of the concept for the answer.
I am saving some data in FirebaseDatabase under a child as a key:value pair.
The problem is that data is getting saved as pushed, like if data1 is saved already than data2 will get saved below it. I want to save it above this already saved data.
Here is the data structure:
- branch
- child
- uniqueKey1: data1
- uniqueKey2: data2
I am saving the data using this code:
String key = mDatabase.child("branch").child("child").push().getKey();
mDatabase.child("branch").child("child").child(key).setValue(data);
What I want is the structure below:
- branch
- child
- uniqueKey2: data2
- uniqueKey1: data1
How to save the newly added data above the already saved data? Please let me know.
In your firebase database, childs in a node are ordered in alphabetical order, no matter which order you save them.
If you want to retrieve them later in some specific order, let's say order by date added, you might want to create a timestamp reference.
Check here:
How to sort value inside a Firebase child to the rest?
Edit: a lot of answers and edited questions while writing my answer, but as others mentioned you should not worry about the order you see the data in the database, you should only care to provide the right structure to retrieve the data correctly.
As I said, in the DB the childs are ordered in alphabetical order so if you insist on ordering it by date added you should figure out a way to update the key accordingly and then update the whole node.
I don't think the order in which you save data is important as long as you have a strategy to retrieve it correctly in your order of choice. Firebase provides API to retrieve data ordered by either key or value.
Work with Lists of Data on Android
If you just want to retrieve the record first which was added last, you can put a timestamp that accompanies your data. There are also methods that gets you last record of a collection.
What you are trying to do is update data1. Then what you have to do is,
Get reference of the node you want to update.
DatabaseReference ref = mDatabase.child("branch").child("child").child("data1");
then update the child node. I'm assuming there is a child name under data1, whose value is 'Foo'. And now you want to update it 'Bar'.
Hashmap<String, String> map = new Hashmap();
map.put("name","Bar");
ref.updateChildren(map);
This is how you update a node. If you want to completely replace the node, then you can delete the node and by calling removeValue() method and pushing the data again!.
I am writing a card-game application.
I represent the Deck of cards using an ArrayList<ImageView>, and i represent the "cards stacking up on the table"(For rollback purposes) in a Stack<ImageView>.
How can I store the state of both the ArrayList<ImageView> and the Stack<ImageView> in order to pick up from where i left off earlier.
I can only think of SharedPreferences, which only supports primitives and Sets.
Your might want to consider using a SQLite database for this. Your columns would be the properties on the object you want to store. If you add on "order" property you can then use the SQL logic
order by `order` DESC
at the end of your query to get them back in the correct order.
Here's a guide:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/