Skip to content

Commit 730c01c

Browse files
author
CodeBuddy Attribution Bot
committed
fix(attribution): MCP writeNoSqlDatabaseContent 缺少嵌套对象部分更新的清晰文档与示例,易导致整块替换而非局部更新 (issue_mojxi9ss_hae7qn)
1 parent 6eaebcb commit 730c01c

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

mcp/src/tools/databaseNoSQL.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ describe("NoSQL database tools", () => {
250250
expect(meta.description).toContain("MongoDB updateOne/updateMany");
251251
expect(meta.description).toContain("`$set`、`$inc`、`$push`");
252252
expect(meta.description).toContain("`_id` 就是该 `uid`");
253-
expect(meta.description).toContain("`address.city`");
253+
expect(meta.description).toContain("`shipping.city`");
254254
expect(meta.inputSchema.update.description).toContain("MgoUpdate");
255255
expect(meta.inputSchema.update.description).toContain("`$set`");
256256
expect(meta.inputSchema.update.description).toContain("`status`");
257-
expect(meta.inputSchema.update.description).toContain("`address.city`");
257+
expect(meta.inputSchema.update.description).toContain("`shipping.city`");
258258
});
259259

260260
it("writeNoSqlDatabaseContent should warn when auth-linked role docs are upserted by uid query", async () => {

mcp/src/tools/databaseNoSQL.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ deleteCollection: 删除集合`),
773773
{
774774
title: "修改 NoSQL 数据库数据记录",
775775
description:
776-
"修改 NoSQL 数据库数据记录。⚠️ 本工具为服务端管理工具,用于管理端/运维端操作(如后台脚本、数据迁移、批量修改)。当任务要求编写客户端应用代码时(例如「用 JS SDK 登录并插入数据」、「在前端读写数据库」),不应使用本工具,而应在项目代码中编写 @cloudbase/js-sdk 客户端代码(如 app.database().collection().add()、db.collection().where().get() 等)。可按 MongoDB updateOne/updateMany 的心智模型理解:部分更新必须使用 `$set`、`$inc`、`$push` 等更新操作符;如果直接传「字段到值的普通对象」这类内容,底层会把它当作替换内容,存在覆盖整条文档的风险。更新嵌套对象中的某个字段时必须使用点号路径,例如把 `address.city` 设为 `shenzhen`;如果把整个 `address` 对象作为 `$set` 的值传入,则整个 `address` 对象会被替换,同级其他字段将丢失。若集合中的角色/档案文档会在前端通过 `db.collection(...).doc(uid)` 读取,请确保文档 `_id` 就是该 `uid`;不要用按 `uid` 条件查询再配合 `upsert=true` 的方式去更新 `users` / `profiles`,否则经常会生成一个不同的 `_id`,导致后续 `doc(uid)` 读取命中不到。",
776+
"修改 NoSQL 数据库数据记录。⚠️ 本工具为服务端管理工具,用于管理端/运维端操作(如后台脚本、数据迁移、批量修改)。当任务要求编写客户端应用代码时(例如「用 JS SDK 登录并插入数据」、「在前端读写数据库」),不应使用本工具,而应在项目代码中编写 @cloudbase/js-sdk 客户端代码(如 app.database().collection().add()、db.collection().where().get() 等)。可按 MongoDB updateOne/updateMany 的心智模型理解:部分更新必须使用 `$set`、`$inc`、`$push` 等更新操作符;如果直接传「字段到值的普通对象」这类内容,底层会把它当作替换内容,存在覆盖整条文档的风险。⚠️ 嵌套对象部分更新务必使用点号路径:要更新 `shipping.city`,应传 `$set: {\"shipping.city\": \"guangzhou\"}`,绝不能传 `$set: {\"shipping\": {\"city\": \"guangzhou\"}}`(后者会丢失 shipping 下的其他字段)。若集合中的角色/档案文档会在前端通过 `db.collection(...).doc(uid)` 读取,请确保文档 `_id` 就是该 `uid`;不要用按 `uid` 条件查询再配合 `upsert=true` 的方式去更新 `users` / `profiles`,否则经常会生成一个不同的 `_id`,导致后续 `doc(uid)` 读取命中不到。",
777777
inputSchema: {
778778
action: z
779779
.enum(["insert", "update", "delete"])
@@ -797,7 +797,11 @@ deleteCollection: 删除集合`),
797797
.union([z.object({}).passthrough(), z.string()])
798798
.optional()
799799
.describe(
800-
"更新内容(对象或字符串,推荐对象)(update 操作必填)。按 MongoDB 更新语义传入 MgoUpdate:部分更新请使用 `$set`、`$inc`、`$unset`、`$push` 等操作符,例如使用 `$set` 更新 `status`;不要直接传“字段到值的普通对象”,否则可能替换整条文档。更新嵌套字段时必须使用点号路径,例如通过 `$set` 更新 `address.city`;不要把整个 `address` 对象作为 `$set` 的值传入,否则会替换整个 `address` 对象。",
800+
`更新内容(对象或字符串,推荐对象)(update 操作必填)。按 MongoDB 更新语义传入 MgoUpdate:部分更新请使用 \`$set\`、\`$inc\`、\`$unset\`、\`$push\` 等操作符,例如使用 \`$set\` 更新 \`status\`;不要直接传"字段到值的普通对象",否则可能替换整条文档。
801+
802+
⚠️ 嵌套字段必须用点号路径(如 \`shipping.city\`),禁止整对象替换:
803+
- ❌ 错误:{ "$set": { "shipping": { "city": "guangzhou" } } } — shipping 被整块替换,原有 address/province 等字段全部丢失
804+
- ✅ 正确:{ "$set": { "shipping.city": "guangzhou" } } — 仅更新 city,shipping 下其他字段保留`,
801805
),
802806
isMulti: z
803807
.boolean()

0 commit comments

Comments
 (0)