更新Cloud Firestore文件中的个别地图 [英] Update individual map in cloud firestore document
问题描述
最终更新我从使用基于下面来自andresmijares的答案的事务更改为使用set().
Final Update I changed from using a transaction based on the answer below from andresmijares, to using set().
现在,这使我可以将数据写入DB.
This now allows me to write the data to the DB.
var gradeDocRef = db.collection("students").doc(studentId);
console.log(gradeDocRef);
var setWithMerge = gradeDocRef.set({
"UnitGrades": {
[unitNo]: {
"CG": CG,
"PG": PG,
"TG": TG
}
}
}, { merge: true });
修改我根据下面的andresmijares的评论更改了交易代码.
Edit I altered code for the transaction based on the comment from andresmijares below.
transaction.set(gradeDocRef,{merge:true},{
但是却出现此错误?
传递给函数Transaction.set()的未知选项'UnitGrades'.可用选项:merge,mergeFields
我有一个包含集合学生的云Firestore数据库.每个学生集合都包含一个学生文档,其中包含如下图和子图
I have a cloud firestore database that contains a collection students. Each student collections contains a student document with a map and submap like below
UnitGrades:
{
IT1:
{
CG: "F"
PG: "F"
TG: "F"
id: "IT1"
name: "Fundamentals of IT"
type: "Exam"
}
地图UnitGrades中有10个单位每个学生都有相同的单元组合
I have 10 units within the map UnitGrades Each student has the same combination of units
我想基于Bootstrap中的HTML表单更新地图(该表单可以正常工作,而且很长,因此请不要将其放在此处)
I want to update the map based upon an HTML form in bootstrap (the form is working, plus is quite long so not put it in here)
即改变学生成绩
我使用了firestore交易更新文档,并做了一些修改以从HTML表单中获取数据.
I have used the firestore transaction update documentation and adapted slightly to take in data from a form in HTML.
let studentId = $(this).attr("data-student-id");
let unitNo = $(this).attr("data-unit");
let CG = $(this).attr("data-CG");
let PG = $(this).attr("data-PG");
let TG = $(this).attr("data-TG");
// Create a reference to the student doc.
var gradeDocRef = db.collection("students").doc(studentId);
console.log(gradeDocRef);
return db.runTransaction(function(transaction) {
// This code may get re-run multiple times if there are conflicts.
return transaction.get(gradeDocRef).then(function(gradeDoc) {
if (!gradeDoc.exists) {
throw "Document does not exist!";
}
// update the grades using a transaction
transaction.update(gradeDocRef, {
// in here is my error, I need to be able to select the map
// for the variable for UnitNo only and not wipe the other maps
"UnitGrades": {
[unitNo]: {
"CG": CG,
"PG": PG,
"TG": TG }
});
});
}).then(function() {
console.log("Transaction successfully committed!");
}).catch(function(error) {
console.log("Transaction failed: ", error);
console.log(studentId);
});
我实现的代码更新了正确的单元图,但随后擦除了其余的UnitGrades.我真正想要的是更新变量
The code I have implemented updates the correct unit map but then wipes the rest of the UnitGrades. What I really want is to update the unit map identified in the variable
UnitNo
,然后其余单元保持不变.
UnitNo
and then leave the rest of the units untouched.
例如当前,如果我更新IT1,则可以正确更新地图中的等级,然后从UnitGrades地图中擦除单位IT2,IT3,IT12等.我真的希望IT2,IT3,IT12等保持原样,并使用新值来更新IT1.例如,"F"更改为"P"
e.g. Currently if I update IT1 this correctly updates the grades within the map but then wipes units IT2, IT3, IT12 etc. from the UnitGrades map. I really want IT2, IT3, IT12 etc. to remain in place untouched and IT1 to be updated with the new values. e.g "F" changes to "P"
推荐答案
更改这些行:
transaction.update(gradeDocRef, {
"UnitGrades": {
[unitNo]: {
"CG": CG,
"PG": PG,
"TG": TG }
});
为此
transaction.set(gradeDocRef, {
`UnitGrades.${unitNo}`: {
"CG": CG,
"PG": PG,
"TG": TG
}, { merge: true });
据我所知,它的工作方式如下:
假设您的文档如下所示:
assuming you doc looks like this:
{
"fantasticsFours": {
"thing": { ... },
"susan": { ... },
"mister": { ... }
}
}
我们需要添加 {"humanTorch":{...}}
已设置并合并
db.collection('heroes').doc(`xxxXXXxxx`).set({
"fantasticsFours": {
"humanTorch":{ ... }
}
}, {merge:true})
将得到以下数据:
{
"fantasticsFours": {
"thing": { ... },
"susan": { ... },
"mister": { ... },
"humanTorch":{ ... }
}
}
使用更新
db.collection('heroes').doc(`xxxXXXxxx`).update({
"fantasticsFours": {
"humanTorch":{ ... }
}
})
将得到以下数据:
{
"fantasticsFours": {
"humanTorch":{ ... }
}
}
更多此处
这篇关于更新Cloud Firestore文件中的个别地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!