diff --git a/docs/en/guide/db/index.md b/docs/en/guide/db/index.md index 117a7dbc35..5fe227ce5e 100644 --- a/docs/en/guide/db/index.md +++ b/docs/en/guide/db/index.md @@ -145,4 +145,20 @@ export async function main(ctx: FunctionContext) { ### Null Type -Null is equivalent to a placeholder that indicates a field exists but has no value. \ No newline at end of file +Null is equivalent to a placeholder that indicates a field exists but has no value. + +## Intelligent Migration of `cloud.database()` to Native Queries + +We can intelligently rewrite cloud functions using `cloud.database()` to utilize native `MongoDB` queries through the following method: + +The prompt is as follows: paste the Laf cloud function at the end and send it together to an AI (such as Laf Pilot). + +```text +You are a senior programmer, especially skilled in using the official MongoDB SDK under the TypeScript language. Below, I will provide you with a segment of code where the 'db' was originally initialized using cloud.database(). Now, please initialize all instances of 'db' using cloud.mongo.db (this is a variable, not a function), and transform all database operations to conform to the official MongoDB SDK format. + +Note: +1. db=cloud.mongo.db; this statement retrieves an already initialized data connection. +2. Regarding the 'id': previously, the 'db' used a string type for 'id', while MongoDB's official 'id' is of the ObjectId type. If necessary, you will need to add parsing/encapsulation conversion logic. +3. Do not modify other parts of the program's code. +Below is the code: +``` diff --git a/docs/guide/db/index.md b/docs/guide/db/index.md index 1e5fde5bf4..768d479367 100644 --- a/docs/guide/db/index.md +++ b/docs/guide/db/index.md @@ -146,3 +146,20 @@ export async function main(ctx: FunctionContext) { ### Null 类型 Null 相当于一个占位符,表示一个字段存在但是值为空。 + +## 智能迁移 `cloud.database()` 至原生查询 + +我们可以使用如下的方法通过 AI 将 `cloud.database()` 写法的云函数智能改写为 `MongoDB` 原生查询: + +Prompt 如下,将 Laf 云函数粘贴到末尾,一同发送给 AI(如 Laf Pilot)即可: + +```text +你是一个资深程序员,尤其擅长使用TypeScript语言下的官方mongodb sdk。下面我将给你一段代码,代码中的db原来是用cloud.database()初始化的。 +现在,请你将所有的db全部使用cloud.mongo.db初始化(这是一个变量,不是一个函数),并且将所有的数据库操作改成符合官方mongodb sdk的形式。 +注意: + +1. db=cloud.mongo.db,这条语句获取的已经是一个初始化好的数据连接 +2. 关于id。之前的db使用的id的类型是string,而mongodb官方的id是ObjectId类型,如有必要,你需要增加解析/封装的转换逻辑 +3. 不要改动程序的其他代码部分 +下面是代码: +```