TSR*_*TSR 16 node.js firebase google-cloud-firestore
我想更新这样的文档:
db.collection('users').doc(user_id).update({foo:'bar'})
Run Code Online (Sandbox Code Playgroud)
但是,如果doc user_id不存在,则上面的代码将引发错误.因此,如果不存在,如何告诉Firestore创建学生,换句话说,行为如下:
db.collection('users').doc(user_id).set({foo:'bar'})
Run Code Online (Sandbox Code Playgroud)
J. *_*Doe 29
我想你想用这个代码:
db.collection('users').doc(user_id).set({foo:'bar'}, {merge: true})
Run Code Online (Sandbox Code Playgroud)
这将使用提供的数据设置文档,并保持其他字段不变.
如果你需要的东西喜欢created
和updated
时间戳,您可以使用此技术:
let id = "abc123";
let email = "john.doe@gmail.com";
let name = "John Doe";
let document = await firebase.firestore().collection("users").doc(id).get();
if (document && document.exists) {
await document.ref.update({
updated: new Date().toISOString()
});
}
else {
await document.ref.set({
id: id,
name: name,
email: email,
created: new Date().toISOString(),
updated: new Date().toISOString()
}, { merge: true });
}
Run Code Online (Sandbox Code Playgroud)
如果它不存在created
和updated
时间戳,这将创建文档,但updated
如果存在,则仅更改时间戳。
归档时间: |
|
查看次数: |
6014 次 |
最近记录: |