更新单个文档
PATCH/collections/:collectionName/documents/:docId
根据文档 _id 更新单个文档。支持所有更新操作符($set、$inc、$push 等)。
使用 returnDoc=true 可返回更新后的完整文档。如果文档不存在且 upsert=false,返回 404。
Request
Path Parameters
collectionName stringrequired
集合名称
docId stringrequired
文档 ID(支持 ObjectId 字符串和普通字符串)
- application/json
Body
required
data objectrequired
更新数据(EJSON 对象),支持更新操作符
replaceMode boolean
是否替换整个文档
upsert boolean
不存在时是否创建
returnDoc boolean
是否返回更新后的文档
transactionId string
事务 ID
Responses
- 200
- 404
更新成功
- application/json
- Schema
- Example (from schema)
- without_doc
- with_doc
Schema
updated integer
更新的文档数量
matched integer
匹配的文档数量
upsert_id string
upsert 创建的文档 ID(如有)
doc object
更新后的文档(仅 returnDoc=true 时返回)
{
"updated": 0,
"matched": 0,
"upsert_id": "string",
"doc": {}
}
不返回文档
{
"updated": 1,
"matched": 1,
"upsert_id": ""
}
返回更新后的文档(returnDoc=true)
{
"updated": 1,
"matched": 1,
"upsert_id": "",
"doc": {
"_id": {
"$oid": "507f1f77bcf86cd799439011"
},
"name": "John",
"age": {
"$numberInt": "31"
}
}
}
文档不存在
- application/json
- Schema
- Example (from schema)
Schema
code string
错误码
message string
错误信息
request_id string
请求唯一 ID
{
"code": "INVALID_PARAM",
"message": "param.query can't be empty",
"request_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
Loading...