如何更新 Firestore 中唯一更改的字段?
How to update the only changed fields in Firestore?
在 Firestore 的 users
集合中,我将所有用户的 uid
作为文档,在每个用户文档中我都存储了用户首选项。
例如,这是我保存在特定用户文档中的用户首选项示例:
{
"preferences": {
"settings": {
"themeColorMode": "light-mode",
"debugMode": false
},
"filterChips": {
"pathName": {
"filterChipsPreferences": true
}
}
}
}
我想用 API
正文中发送的数据更新用户文档
我希望 API 以某种方式兼容
- 除了首选项,我应该可以添加另一个根节点
- 我应该能够在
preferences.settings
& preferences.filterChips
中自定义和添加新节点
- 我应该能够更新特定节点 - 示例:
preferences.settings.themeColorMode
& preferences.filterChips.filterChipsPreferences
例如,在请求正文中我发送此信息:
{
"preferences": {
"settings": {
"themeColorMode": "dark-mode",
"isSoundNotificationOn": false,
"isAppListeningToStream": true
},
"filterChips": {
"pathName": {
"filterChipsPreferences": false
},
"FirstUsedSearch": "23/12/2021"
},
"columnDefs": {
"pathName": {
"ColumnDefsPreferences": true
}
},
},
"search": {
"savedSearches":["searchName"]
}
}
我希望将此结果保存在用户的文档中
{
"preferences": {
"settings": {
"themeColorMode": "dark-mode",
"isSoundNotificationOn": false,
"isAppListeningToStream": true,
"debugMode": false
},
"filterChips": {
"pathName": {
"filterChipsPreferences": false
},
"FirstUsedSearch": "23/12/2021"
},
"columnDefs": {
"pathName": {
"ColumnDefsPreferences": true
}
},
"search": {
"savedSearches":["searchName"]
}
}
}
我该如何处理?
由于你是在调用Firestore的服务器上使用Node.js Admin SDK,其实很简单:你需要使用DocumentReference
的update()
方法。
The update()
method accepts either an object with field paths encoded
as keys and field values encoded as values, or a variable number of
arguments that alternate between field paths and field values.
更准确地说:
// 1/ Define your DocumentReference
const userId = ...;
const docRef = admin.firestore().doc(`users/${userId}`);
// 2/ Get the desired elements from the payload received by your API and use them to build the object to pass to the update method
// For example
const updateObj = {
preferences.settings.debugMode: false,
preferences.settings.themeColorMode: "myColor",
preferences.filterChips.filterChipsPreferences: "myPref",
aNewRootNode: {
foo: 'bar',
bar: 'foo'
}
}
await docRef.update(updateObj);
有关如何更新嵌套对象中的字段的更多信息,请参阅 doc of the JS SDK。
在 Firestore 的 users
集合中,我将所有用户的 uid
作为文档,在每个用户文档中我都存储了用户首选项。
例如,这是我保存在特定用户文档中的用户首选项示例:
{
"preferences": {
"settings": {
"themeColorMode": "light-mode",
"debugMode": false
},
"filterChips": {
"pathName": {
"filterChipsPreferences": true
}
}
}
}
我想用 API
正文中发送的数据更新用户文档我希望 API 以某种方式兼容
- 除了首选项,我应该可以添加另一个根节点
- 我应该能够在
preferences.settings
&preferences.filterChips
中自定义和添加新节点
- 我应该能够更新特定节点 - 示例:
preferences.settings.themeColorMode
&preferences.filterChips.filterChipsPreferences
例如,在请求正文中我发送此信息:
{
"preferences": {
"settings": {
"themeColorMode": "dark-mode",
"isSoundNotificationOn": false,
"isAppListeningToStream": true
},
"filterChips": {
"pathName": {
"filterChipsPreferences": false
},
"FirstUsedSearch": "23/12/2021"
},
"columnDefs": {
"pathName": {
"ColumnDefsPreferences": true
}
},
},
"search": {
"savedSearches":["searchName"]
}
}
我希望将此结果保存在用户的文档中
{
"preferences": {
"settings": {
"themeColorMode": "dark-mode",
"isSoundNotificationOn": false,
"isAppListeningToStream": true,
"debugMode": false
},
"filterChips": {
"pathName": {
"filterChipsPreferences": false
},
"FirstUsedSearch": "23/12/2021"
},
"columnDefs": {
"pathName": {
"ColumnDefsPreferences": true
}
},
"search": {
"savedSearches":["searchName"]
}
}
}
我该如何处理?
由于你是在调用Firestore的服务器上使用Node.js Admin SDK,其实很简单:你需要使用DocumentReference
的update()
方法。
The
update()
method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values.
更准确地说:
// 1/ Define your DocumentReference
const userId = ...;
const docRef = admin.firestore().doc(`users/${userId}`);
// 2/ Get the desired elements from the payload received by your API and use them to build the object to pass to the update method
// For example
const updateObj = {
preferences.settings.debugMode: false,
preferences.settings.themeColorMode: "myColor",
preferences.filterChips.filterChipsPreferences: "myPref",
aNewRootNode: {
foo: 'bar',
bar: 'foo'
}
}
await docRef.update(updateObj);
有关如何更新嵌套对象中的字段的更多信息,请参阅 doc of the JS SDK。