Obi*_*i E 6 firebase google-cloud-firestore
如果 Firestore 文档不存在,我想创建它们 - 如果它们确实存在,请跳过它们(不要更新)。这是流程
var arrayOfRandomIds = [array of 500 random numbers];
for (var id of arrayOfRandomIds)
{
var ref = db.collection("tickets").doc(id);
batch.set(ref, {name: "My name", location: "Somewhere"}, { merge: true });
}
batch.commit();
Run Code Online (Sandbox Code Playgroud)
我只想知道,如果存在,这会覆盖任何现有文件吗?我不想覆盖任何内容,只是跳过了。
谢谢。
同时还有“创建但不覆盖”功能。假设您使用的是 JavaScript,这里是参考:https ://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#create
这是文档中相应的示例代码:
let documentRef = firestore.collection('col').doc();
documentRef.create({foo: 'bar'}).then((res) => {
console.log(`Document created at ${res.updateTime}`);
}).catch((err) => {
console.log(`Failed to create document: ${err}`);
});
Run Code Online (Sandbox Code Playgroud)
使用.create()代替.set()应该可以为您解决问题,而无需依赖应用程序逻辑的安全规则。
我认为您可以使用安全规则来实现这一点。这样,您无需为阅读额外的文档以查看它是否已经存在而付费。
service cloud.firestore {
match /databases/{database}/documents {
match /tickets/{id} {
allow create;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2979 次 |
| 最近记录: |