更新文档,如果不存在则创建

collection.doc().set()

注意:

  • 此方法会覆写已有字段,需注意与update表现不同,比如以下示例执行set之后follow字段会被删除
  1. collection.doc('doc-id').set({
  2. name: "Hey",
  3. count: {
  4. fav: 1
  5. }
  6. }).then(function(res) {
  7. });
  1. // 更新前
  2. {
  3. "_id": "xxx",
  4. "name": "Hello",
  5. "count": {
  6. "fav": 0,
  7. "follow": 0
  8. }
  9. }
  10. // 更新后
  11. {
  12. "_id": "xxx",
  13. "name": "Hey",
  14. "count": {
  15. "fav": 1
  16. }
  17. }