Difference between two maps when updating field in cloud firestore - firebase

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.

Related

With Google AppScripts, Exporting data from firebase to google sheet's data have data type with the value

I'm using Google App Scripts and Firestore GoogleAppScripts for data fetching from firestore to google sheet as explained on this library.
Here's my code.
function getGemExchange() {
// 1. Get a Firestore instance
const firestore = getFirestore()
// 2. Get a collection from Firestore
const userDocuments = firestore.getDocuments('gemExchangeRequests').map(document => document.fields)
// 3. Get the first document from the collection
const first = userDocuments[0];
const columns = Object.keys(first);
const sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(columns);
// 4. Turn each document into an array to be appended to the sheet
userDocuments.forEach(document => {
const row = columns.map(column => document[column])
sheet.appendRow(row)
})
}
It all works fine except the values have its data type with it.
status taxAmount bankDetails gemAmount uniqueId serviceFee
{stringValue=PENDING} {doubleValue=0.02} {stringValue=U2FsdGVkX1/a2sX4CaKi0Ham/dSSEzlg7QNca236+clZbRDwHZwe8C7Xhy53ybk0c6l34CLYtQ+0KFnDOuAAfUc4WUKRfQTPWxS8NDHP2kREeRzX2BJGZ6JpL3NfxmsbR/dGpvKqy/Pa+Of3guJmjg==} {integerValue=33} {stringValue=APR-TC0} {integerValue=2}
So I want to get rid of data types such as "stringValue", "integerValue" or "doubleValue" on my Google Spreadsheet.
For example, I want "PENDING" for the "status" column, not "{stringValue=PENDING}"
What should I do in this case?
In your situation, how about the following modification?
From:
userDocuments.forEach(document => {
const row = columns.map(column => document[column])
sheet.appendRow(row)
})
To:
const values = userDocuments.map(document => columns.flatMap(column => Object.values(document[column])));
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
In this modification, each value is retrieved from document[column]. And, the values are put using setValues instead of appendRow. By this, the process cost will be reduces a little. Ref
Note:
I thought that in your script, the arrange of the header row might be changed every run. So, when you want to constant the header row, I would like to propose to declare the header row as a variable in the script.
References:
setValues(values)
Object.values()

How to delete the array elements at index in firestore in javascript

var arr = [ "GKRLrcpSL2BmKx3BfvWj","fMwH404fweoFJJqyE5Nf" , "DTurwbw64CWw4WosUQtC" ] //
arr.forEach((element) => {
console.log(element);
db.collection("Check").doc(element).get().then((docs) => {
var data = docs.data();
var arr = data.fill;
arr.forEach((item) => {
if(item['check'] == 'a') {
db.collection("Check").doc(element).update({
fill: firebase.firestore.FieldValue.arrayRemove('check')
}).then((val) => {console.log("Updated")})
}
});
});
});
I have a collection Check which consist of an array fill and there are objects in the fill i want to match the data in the fill and delete the value that index in firestore
FieldValue.arrayRemove() only works with the complete value of the array item to remove. It won't work for removing items by index. In fact, Firestore doesn't offer any way to directly modify array items by index.
What you'll have to do instead is read the document, modify the "fill" array in memory as you see fit, then update the document with the new value back to the document.
If that works for you, use a map instead of an array, putting dummy values if necessary. Then you can remove the field directly.
fill: {
a: xxx,
b: xxx,
check: xxx
}
db.collection("Check").doc(element).update({
fill.check: firebase.firestore.FieldValue.delete()
})
My solution is based on specifying the item ID to remove, example:
firebase
.firestore()
.collection('projects') // Collection
.doc(project.id) // ID string
.delete() // Promise

Retrieve id using multiple value From Firebase

I want to retrieve item id and get it to variable using items other value. according to this image ("get id where Brand="Manchee" and ItemName="Cream Cracker" and SubCategory="100g" ")
how write this function
Here you can try this solution.I think you wanted to query the database so you can fire query on your database based on your variable value.Just pass the key and value in this query and you can have all the data reside in the table which falls within this query.
var ref = firebase.database().ref("items");
ref.orderByChild("itemName").equalTo('Manchee').once("value", (items : any)=> {
console.log(items.key);
console.log(items.val());
let itemArray: any = [];
items.forEach((item) => {
itemArray.push({
key: item.key,
});
});
this.items = itemArray;
console.log("Prescription Data: ", itemArray);
});

How to update nested object of firebase data in (angularfire2 / Ionic), and also i want to update with given value not with key

I was integrate firebase with ionic3 angularjs, adding data successfully like
var fireData =
{
userid : '1122233',
questionId : '18022',
answerId : '25633',
points : '2'
}
//Add
this.sample.list('paperCode').push(fireData);
Now I want to update like below mentioned image
If you don't know the key, first query the data with the field you know then iterate through the result and get the key. Then you can perform update.
Try
updateFunction(questionId){
this.sample.list('/paperCode', ref => ref.orderByChild('questionId').equalTo(questionId)).snapshotChanges()
.subscribe(actions => {
actions.forEach(action => {
// here you get the key
console.log(action.key);
this.sample.list('/paperCode').update(action.key, { points: 10 });
});
});
}
I hope your question id is unique, otherwise it will override all queried results.
You need to set a path to that object and update the field you would like. In this example...
firebase.database().ref(1710172911409/L5F4BEuePS8qxUqDNoF/questionId).update("NEWQUESTIONID);
If you don't know the unique id of the pushed object, you can query for it. Learn more here

Is a variable for a field name allowed during update in Meteor?

I have been trying to update a collection i have.Problem is,i dont know the field name so am relying on some logic to come up with the field name.
For instance
Er.update({ _id: "BCwrQEdiTr9GMmKWW" }, {
$set: {
"x" : y
}
});
where x is the field name.
This is my code
var obj = {};
obj[x] = y;
Er.update({ _id: "BCwrQEdiTr9GMmKWW" }, {$set: {obj}});
I am getting this error
update failed: MongoError: The dotted field 'ersubjects.0.English' in
'obj.ersubjects.0.English' is not valid for storage.
English is a field under ersubjects so i want to update it this way ersubjects.0.English and it works on mongo.
Why is this not working in meteor?.
You can't store documents that have a dot in the key. See this answer for an explanation.
What you can do is use lodash's extremely handy _.set function to create your object with dynamic keys like this:
var obj = {};
var variableKey = 'ersubjects';
_.set(obj, [variableKey, 0, 'English], 'someValue');
Now you can safely store this object to Mongo.

Resources