pushing multiple values to the same database location without overwriting existing data - firebase

How do I append new values to an array in realtime database, for example appending new images urls to an array.
"1":{
"title":"some title",
"imgs": [
"https://img1",
"https://img2",
"https://img3",
"https://img4"
]
}
the push() methods adds unique keys which I dont want.
This does overwrite all the data in the imgs array:
set(ref(db, postId +'/imgs'), {
document.getElementById('input_img_url').value,
})

Related

Difference between two maps when updating field in cloud firestore

var data = {
'key.number': 1,
};
var data = {
'key' : {
'number': 1,
}
};
Is there any difference between the two Maps when updating number on the cloud firestore using
var ref = Firestore.instance.document('users/id');
ref.updateData(data);
If the document didn't already have a "key" field, there is no difference.
If the document did already have a "key" field, the first one would just overwrite any existing value for key.field, preserving any other fields in the key object, and the second one would completely overwrite the key object and all of its existing fields.

FIrebase setData kef-value sequence not preserved

I am trying to add data in firebase as the key-value sequence I have provided in setData.
create({Function onAdded, Function onDuplicate}) async {
bool temp = await lrCheck();
if (temp) {
await fireStore.collection("Admin").document(TextFieldData.lrNO).setData(
{
"lrNO": TextFieldData.lrNO,
"departureDate": TextFieldData.departureDate,
"partyName": TextFieldData.partyName,
"vehicleNo": TextFieldData.vehicleNo,
"origin": TextFieldData.origin,
"destination": TextFieldData.destination,
"quantity": TextFieldData.quantity,
"invoiceNo": TextFieldData.invoiceValue,
"invoiceValue": TextFieldData.invoiceValue,
"arrivalDate": TextFieldData.arrivalDate,
"materialType": TextFieldData.materialType,
"phoneNo": TextFieldData.phoneNo,
},
).whenComplete(onAdded());
But the data stored in firebase is in alphabatical orer of keys plzz help.
ex:
arrivalDate: value
destination: value
and so on ....
Fierstore does not set an order for the fields in a document. If you need an order, you will have to apply that in your app code. If you have an array of ordered data to store, consider putting that data in a single list type field instead.
The data in the firebase document are stored in key-value pairs so, There should be no problem even if the data is received in unordered fashion as long as you are mapping the data correctly in your model.

How can I update nested List in Map that's all inserted to a List? Eg. List<Map<String, dynamic>>

I am trying to update or add to the end of the Lists that are in a Map and all inserted into a List that contains those maps. The List name is 'classes', what I have tried doing was using dot notation, so classes.index.example, but that doesn't work. Why...let's say I have two indexes in the list, If I go ahead and update index 0, the 'questions' and 'answers' will get inserted into that correct index, But for some reason, it will delete index 1 and any other that was created. It's as if It's overwriting all the data, but I don't understand why, I am not using 'setData()' Also, if I leave 'title' out, that too will get deleted??
Future updattingUserData(int index, List<dynamic> question, List<dynamic> answer, String title) async {
return await _collref.document(uid).updateData({
"classes.$index.questions": FieldValue.arrayUnion(question),
"classes.$index.answers": FieldValue.arrayUnion(answer),
//"classes.$index.title": title
});
}
Firestore doesn't have the capability of changing an array element knowing only its index within an array field. What you will have to do is read the document, modify the classes array in memory, then update the entire classes array field back to the document.

How to update only part of the JSON?

I have a DynamoDB table:
+--------------------------------------------------+
| Customer ID (Primary Key)|Gamestats (JSON entry) |
+--------------------------------------------------+
JSON:
{
"Gamestats": [
{
"ID": "QuickShootingMode",
"status": 1
},
{
"ID": "FastReloadMode", // Just want to update this and not update the entire JSON
"status": 0
}
],
"CustomerID": "xyz"
}
I want to update only parts of the JSON. What is the best way to do it? Eg, update the QuickShootingMode to be false.
One way is to make a call and fetch the JSON and then Iterate the JSON and update the value and then put the new JSON back in dynamo DB. It means it would make 2 calls
A) to get the data and
B) to put the data in DB.
Is there a better way by which I could directly update the data and avoid making these extra network calls? I could convert each key of the JSON to be a column in dynamo BD, but if the number of keys grows then I’ll end up having lots of column (which might be a bad design), hence I think having the JSON saved in one column Game stats would make more sense.
Map<String, AttributeValue> key = new HashMap<>();
AmazonDynamoDB dynamoDB = dynamoDBClient.getDynamoDB();
key.put(USER_ID_KEY, new AttributeValue().withS("xyz"));
key.put("Gamedata", new AttributeValue().withS("some JSON"));
PutItemRequest request = new PutItemRequest()
.withTableName(table)
.withItem(key);
PutItemResult result = dynamoDB.putItem(request);
Is there a better way to achieve what I want?
It looks like from your question you are storing stringified JSON. If so an update won't help you, but as far as I can tell there is no value in storing stringified JSON instead of using dynamodb maps and lists.
You can use an update to set a nested attribute in a map or a list. Using a map instead of a list for the gamestats attribute is better because then you don't have to worry about the order of the attributes.
Javascript example with Gamestats being a map.
dynamodb.update({
TableName: table,
Key: key,
UpdateExpression: 'SET #gs.#qs.#status = :newStatus',
ExpressionAttributeNames: {'#gs': 'Gamestats', '#qs': 'QuickShootingMode', '#status': 'status' },
ExpressionAttributeValues: { ':newStatus': false }
}, callback)

make book.randomID key in amazon dynamodb table

for some reason I want to use book.randomID as key in amazon DynamoDB table using java code. when i tried id added a new field in the item named "book.randomID"
List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
keySchema.add(new KeySchemaElement().withAttributeName("conceptDetailInfo.conceptId").withKeyType(KeyType.HASH)); // Partition
and here is the json structure
{
"_id":"123",
"book":{
"chapters":{
"chapterList":[
{
"_id":"11310674",
"preferred":true,
"name":"1993"
}
],
"count":1
},
"randomID":"1234"
}
}
so is it possible to use such element as key. if yes how can we use it as key
When creating DynamoDB tables AWS limits it to the types String, Binary and Number. Your attribute book.random seems to be a String.
As long as it's not one of the other data types like List, Map or Set you should be fine.
Just going to AWS console and trying it out worked for me:

Resources