跳到主要内容

数据库

getDocument#

1. API 描述#

功能:单文档查询

2. 请求地址#

GET https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents/{docId}?limit={limit}&skip={skip}&fields={fields}&sort={sort}&transactionId={transactionId}
url query 参数#
字段类型必填说明
limitString限制查询的文档数量
skipString偏移的文档数量
fieldsString例:{key1: 1, key2: 0},表示取 key1 字段,丢弃 key2 字段
sortString例:{key1: 1, key2: -1},表示按 key1 升序,key2 降序 排序
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
offsetnumber偏移量
limitnumber限制值
listArray<String>文档数组,数组单元项为 EJSON 序列化字符串

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${docId}`,    method: "GET",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    consol.log(data);  });

updateDocument#

1. API 描述#

功能:单文档更新

2. 请求地址#

PATCH https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents/{docId}?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
dataStringEJSON 序列化后的替换文档字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
updatedNumber更新成功的文档数量
matchedNumber符合条件的文档数量
upsert_idStringupsert 更新时存在,其余情况为 null

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${docId}`,    method: "PATCH",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      data: EJSON.stringify(        {          name: "new Name" // 更新 name 字段        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

setDocument#

1. API 描述#

功能:单文档替换更新,若指定 docId 的 doc 已存在,则替换更新,否则插入新 doc

2. 请求地址#

PUT https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents/{docId}?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
dataStringEJSON 序列化后的替换文档字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
updatedNumber更新成功的文档数量
matchedNumber符合条件的文档数量
upsert_idString替换更新 插入新 doc 时存在,其余情况为 null

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst unExistDocId = "testUnExistDocId"; // 不存在的文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${docId}`, // 替换更新    // url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${unExistDocId}`, // 插入当前文档    method: "PUT",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      data: EJSON.stringify(        {          name: "new Name" // 更新 name 字段        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated); // 替换更新时存在该值    console.log(response.body.data.upsert_id); // 文档不存在插入文档时,存在该值  });

insertDocument#

1. API 描述#

功能:单文档插入

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents/{docId}?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
dataStringEJSON 序列化后的插入文档字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
insertedIdsArray<String>文档数组,数组单元项为插入成功的 docId

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${docId}`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      data: EJSON.stringify(        {          name: "luke",          sex: "male"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.insertedIds);  });

deleteDocument#

1. API 描述#

功能:单文档删除

2. 请求地址#

DELETE https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents/{docId}?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
deletedNumber删除成功的 doc 数

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${docId}`,    method: "DELETE",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.deleted);  });

insertDocuments#

1. API 描述#

功能:批量插入文档

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
dataArray<String>字符串数组,数组单元项为 EJSON 序列化后的插入文档字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
insertedIdsArray<String>文档数组,数组单元项为插入成功的 docId

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
const insertDatas = [  {    name: "luke"  },  {    name: "jimmy"  }];
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      data: insertDatas.map((item) => EJSON.stringify(item, { relaxed: false }))    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.insertedIds);  });

find#

1. API 描述#

功能:批量查询文档

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents:find?limit={limit}&skip={skip}&fields={fields}&sort={sort}&transactionId={transactionId}
url query 参数#
字段类型必填说明
limitString限制查询的文档数量
skipString偏移的文档数量
fieldsString例:{key1: 1, key2: 0},表示取 key1 字段,丢弃 key2 字段
sortString例:{key1: 1, key2: -1},表示按 key1 升序,key2 降序 排序
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
queryStringEJSON 序列化后的查询字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
offsetnumber偏移量
limitnumber限制值
listArray<String>文档数组,数组单元项为 EJSON 序列化字符串

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          sex: "male"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

count#

1. API 描述#

功能:统计查询文档总数

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents:count?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
queryStringEJSON 序列化后的查询字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
totalnumber总数

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:count`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          sex: "male"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.total);  });

updateOne#

1. API 描述#

功能:批量查询并更新单文档

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents:updateOne?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
queryStringEJSON 序列化后的查询字符串
dataStringEJSON 序列化后的更新字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
updatedNumber更新成功的文档数量
matchedNumber符合条件的文档数量
upsert_idString替换更新 插入新 doc 时存在,其余情况为 null

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          sex: "male"        },        { relaxed: false }      ),      data: EJSON.stringify(        {          name: "newName"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

updateMany#

1. API 描述#

功能:批量查询并批量更新

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents:updateMany?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
queryStringEJSON 序列化后的查询字符串
dataStringEJSON 序列化后的更新字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
updatedNumber更新成功的文档数量
matchedNumber符合条件的文档数量
upsert_idString替换更新 插入新 doc 时存在,其余情况为 null

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateMany`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          sex: "male"        },        { relaxed: false }      ),      data: EJSON.stringify(        {          name: "newName"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

deleteOne#

1. API 描述#

功能:批量查询并删除单文档

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents:deleteOne?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
queryStringEJSON 序列化后的查询字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
deletedNumber删除成功的 doc 数

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:deleteOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          sex: "male"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.deleted);  });

deleteMany#

1. API 描述#

功能:批量查询并批量删除

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/documents:deleteMany?transactionId={transactionId}
url query 参数#
字段类型必填说明
transactionIdStringtransactionId 存在则表示事务中文档操作

3. 请求体#

字段类型必填说明
queryStringEJSON 序列化后的查询字符串

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
dataObject返回结果
codeString错误码
messageString错误信息
data#
字段类型必填说明
deletedNumber删除成功的 doc 数

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:deleteMany`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          sex: "male"        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.deleted);  });

Query Command#

$eq#

表示字段等于某个值。

语法:{ <field>: { $eq: <value> } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          name: {            $eq: "luke" // 查询 name 字段等于 luke 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$ne#

表示字段不等于某个值。

语法:{ <field>: { $ne: <value> } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          name: {            $ne: "luke" // 查询 name 字段不等于 luke 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$gt#

表示字段大于某个值。

语法:{field: {$gt: value} }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          age: {            $gt: 18 // 查询 age 字段大于 18 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$gte#

表示字段大于或等于某个值。

语法:{field: {$gte: value} }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          age: {            $gte: 18 // 查询 age 字段大于等于 18 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$lt#

表示字段小于某个值。

语法:{field: {$lt: value} }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          age: {            $lt: 18 // 查询 age 字段小于 18 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$lte#

表示字段小于或等于某个值。

语法:{field: {$lte: value} }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          age: {            $lte: 18 // 查询 age 字段小于等于 18 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$in#

字段值在给定的数组中。

语法:{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          category: {            $in: ["science", "computer"] // 查询 category 字段属于 science, computer 之一 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$nin#

字段值不在给定的数组中。

语法:{ field: { $nin: [<value1>, <value2>, ... <valueN> ] } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          category: {            $nin: ["science", "computer"] // 查询 category 字段不属于 science, computer 之一 的 doc          }        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$and#

表示需同时满足指定的两个或以上的条件。

语法:{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          $and: [            {              price: {                $lt: 200              }            },            {              price: {                $gt: 100              }            }          ] // 查询 价格 price 字段大于 100 且 小于 200 的 doc        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$or#

表示需满足所有指定条件中的至少一个。

语法:{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          $or: [            {              price: {                $lt: 100              }            },            {              price: {                $gt: 200              }            }          ] // 查询 价格 price 字段小于 100 或 大于 200 的 doc        },        { relaxed: false }      )    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);
    const data = response.body.data.list.map((item) => EJSON.parse(item)); // EJSON 反序列化处理    console.log(data);  });

$nor#

表示逻辑 "都不" 的关系,表示需不满足指定的所有条件。

语法:{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson") // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:find`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp",
      ,    },    body: {        query: EJSON.stringify({                $nor: [{                    price: {                        $lt: 100                    }                }, {                    price: {                        $gt: 200                    }                }] // 查询 价格 price 字段既不小于 100 且 不大于 200 的 doc        }, {relaxed: false})    }    json: true  },  (err, response, body) => {      console.log(err)      console.log(response.statusCode)
      const data = response.body.data.list.map(item => EJSON.parse(item)) // EJSON 反序列化处理      console.log(data)  });

Update Command#

$set#

用于设定字段等于指定值。

语法:{ $set: { <field1>: <value1>, ... } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson") // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp",
      ,    },    body: {        query: EJSON.stringify({            title: 'picture'        }, {relaxed: false}),        data: EJSON.stringify({                $set: {                    style: {                        color: "red",                        size: "large"                    }                }        }, {relaxed: false}) // 查询 title 字段为 picture 的 doc,并更新第一个文档的 style 为 {color:"red", size: "large"}    }    json: true  },  (err, response, body) => {      console.log(err)      console.log(response.statusCode)      console.log(response.body.data.updated)  });

$inc#

用于指示字段自增某个值,这是个原子操作。

语法:{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          category: "computer"        },        { relaxed: false }      ),      data: EJSON.stringify(        {          $inc: {            price: 1000          }        },        { relaxed: false }      ) // 查询 category 字段为 computer 的 doc,并更新第一个文档的 price 自增 1000    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

$mul#

用于指示字段自乘某个值。

语法:{ $mul: { <field1>: <number1>, ... } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson") // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp",
      ,    },    body: {        query: EJSON.stringify({            category: 'computer'        }, {relaxed: false}),        data: EJSON.stringify({                $mul: {                    price: 1.5                }        }, {relaxed: false}) // 查询 category 字段为 computer 的 doc,并更新第一个文档的 price 自乘 1.5倍    }    json: true  },  (err, response, body) => {      console.log(err)      console.log(response.statusCode)      console.log(response.body.data.updated)  });

$unset#

用于表示删除某个字段。

语法:{ $unset: { <field1>: "", ... } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          category: "computer"        },        { relaxed: false }      ),      data: EJSON.stringify(        {          $unset: {            rating: ""          }        },        { relaxed: false }      ) // 查询 category 字段为 computer 的 doc,并删除第一个文档的评分 rating 字段    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

$push#

向数组尾部追加元素,支持传入单个元素或数组。

语法:{ $push: { <field1>: <value1>, ... } }

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          category: "computer"        },        { relaxed: false }      ),      data: EJSON.stringify(        {          $push: {            tags: {              $each: ["tag1", "tag2", "tag3"]            }          }        },        { relaxed: false }      ) // 查询 category 字段为 computer 的 doc,并向第一个文档的 tags 字段 追加 tag1, tag2 ,tag3 3个元素    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

$pop#

删除数组尾部元素。

语法:{ $pop: { <field>: <-1 | 1>, ... } }

1 表示从数组尾部剔除  元素,-1 表示从数组头部剔除元素

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents:updateOne`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {      query: EJSON.stringify(        {          category: "computer"        },        { relaxed: false }      ),      data: EJSON.stringify(        {          $pop: {            tags: 1          }        },        { relaxed: false }      ) // 查询 category 字段为 computer 的 doc,并剔除第一个文档的 tags 字段对应数组的第一个元素    },    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.data.updated);  });

Transaction#

startTransaction#

1. API 描述#

功能:开始事务

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/transaction

3. 请求体#

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
transactionIdString事务 ID
codeString错误码
messageString错误信息

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
request(  {    url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction`,    method: "POST",    headers: {      "X-CloudBase-Authorization": "your authorization",      "X-CloudBase-SessionToken": "your token",      "X-CloudBase-TimeStamp": "the timestamp"    },    body: {},    json: true  },  (err, response, body) => {    console.log(err);    console.log(response.statusCode);    console.log(response.body.transactionId);  });

commitTransaction#

1. API 描述#

功能:提交事务

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/transaction/${transactionId}:commit

3. 请求体#

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
codeString错误码
messageString错误信息

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
async function test() {  // 1. 开始事务  const startTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {},        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);        // console.log(err)        // console.log(response.statusCode)        // console.log(response.body.transactionId)      }    );  });
  // 2. 提交事务
  const commitTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction/${startTransactionRes.transactionId}:commit`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {},        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);        // console.log(err)        // console.log(response.statusCode)        // console.log(response.body.transactionId)      }    );  });
  console.log(commitTransactionRes);}
test();

rollbackTransaction#

1. API 描述#

功能:回滚事务

2. 请求地址#

POST https://tcb-api.tencentcloudapi.com/api/v2/envs/{envId}/databases/{collectionName}/transaction/${transactionId}:rollback

3. 请求体#

4. 响应体#

字段类型必填说明
statusCodeNumber状态码,200
bodyObject响应体,结构如下
body#
字段类型必填说明
requestIdString请求 ID
codeString错误码
messageString错误信息

5. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
async function test() {  // 1. 开始事务  const startTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {},        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);        // console.log(err)        // console.log(response.statusCode)        // console.log(response.body.transactionId)      }    );  });
  // 2. 回滚事务
  const rollbackTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction/${startTransactionRes.transactionId}:rollback`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {},        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);        // console.log(err)        // console.log(response.statusCode)        // console.log(response.body.transactionId)      }    );  });
  console.log(rollbackTransactionRes);}
test();

CRUD in transaction#

1. 使用示例#

// Node 示例const request = require("request"); // 请求库,需npm 安装依赖const envId = "testEnv"; // 环境IDconst collectionName = "testCollection"; // 集合名const docId = "testDocId"; // 文档IDconst { EJSON } = require("bson"); // bson库,需npm 安装依赖
async function test() {  // 1. 开始事务  const startTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {},        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);        // console.log(err)        // console.log(response.statusCode)        // console.log(response.body.transactionId)      }    );  });
  // 2. 事务插入文档  const insertDocId = "lukeTest";  const insertTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${insertDocId}?transactionId=${startTransactionRes.transactionId}`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {          data: EJSON.stringify({            name: "luke"          })        },        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);      }    );  });
  console.log(insertTransactionRes);
  // 3. 事务修改文档  const updateTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${insertDocId}?transactionId=${startTransactionRes.transactionId}`,        method: "PATCH",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {          query: EJSON.stringify({            name: "luke"          }),          data: EJSON.stringify({            name: "jimmy"          })        },        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);      }    );  });
  console.log(updateTransactionRes);
  // 4. 事务查询文档
  const queryTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${insertDocId}?transactionId=${startTransactionRes.transactionId}`,        method: "GET",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);      }    );  });
  console.log(queryTransactionRes);
  // 5. 事务删除文档
  const deleteTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/documents/${insertDocId}?transactionId=${startTransactionRes.transactionId}`,        method: "DELETE",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);      }    );  });
  console.log(deleteTransactionRes);
  // 6. 回滚事务
  const commitTransactionRes = await new Promise((resolve, reject) => {    request(      {        url: `https://tcb-api.tencentcloudapi.com/api/v2/envs/${envId}/databases/${collectionName}/transaction/${startTransactionRes.transactionId}:rollback`,        method: "POST",        headers: {          "X-CloudBase-Authorization": "your authorization",          "X-CloudBase-SessionToken": "your token",          "X-CloudBase-TimeStamp": "the timestamp"        },        body: {},        json: true      },      (err, response, body) => {        if (err) {          reject(err);        }        resolve(response.body);        // console.log(err)        // console.log(response.statusCode)        // console.log(response.body.transactionId)      }    );  });
  console.log(commitTransactionRes);}
test();