提交 dda9d2d4 编写于 作者: 雪洛's avatar 雪洛

docs: clientDB联表查询示例优化

上级 f81f991c
...@@ -108,7 +108,7 @@ let res = await db.collection('table').where({ ...@@ -108,7 +108,7 @@ let res = await db.collection('table').where({
}).get() }).get()
``` ```
### jql查询语法@jsquery ### JQL查询语法@jsquery
`jql`,全称javascript query language,是一种js方式操作数据库的语法规范。 `jql`,全称javascript query language,是一种js方式操作数据库的语法规范。
...@@ -291,34 +291,34 @@ book表内有以下数据,title为书名、author为作者: ...@@ -291,34 +291,34 @@ book表内有以下数据,title为书名、author为作者:
} }
``` ```
order表内有以下数据,book字段为book表的书籍_id,quantity为该订单销售了多少本书: order表内有以下数据,book_id字段为book表的书籍_id,quantity为该订单销售了多少本书:
```js ```js
{ {
"book": "1", "book_id": "1",
"quantity": 111 "quantity": 111
} }
{ {
"book": "2", "book_id": "2",
"quantity": 222 "quantity": 222
} }
{ {
"book": "3", "book_id": "3",
"quantity": 333 "quantity": 333
} }
{ {
"book": "4", "book_id": "4",
"quantity": 444 "quantity": 444
} }
{ {
"book": "3", "book_id": "3",
"quantity": 555 "quantity": 555
} }
``` ```
如果我们要对这2个表联表查询,在订单记录中同时显示书籍名称和作者,那么首先要建立两个表中关联字段`book`的映射关系。 如果我们要对这2个表联表查询,在订单记录中同时显示书籍名称和作者,那么首先要建立两个表中关联字段`book`的映射关系。
即,在order表的db schema中,配置字段 book 的`foreignKey`,指向 book 表的 _id 字段,如下 即,在order表的db schema中,配置字段 book_id`foreignKey`,指向 book 表的 _id 字段,如下
```json ```json
// order表schema // order表schema
...@@ -329,7 +329,7 @@ order表内有以下数据,book字段为book表的书籍_id,quantity为该 ...@@ -329,7 +329,7 @@ order表内有以下数据,book字段为book表的书籍_id,quantity为该
".read": true ".read": true
}, },
"properties": { "properties": {
"book": { "book_id": {
"bsonType": "string", "bsonType": "string",
"foreignKey": "book._id" // 使用foreignKey表示,此字段关联book表的_id。 "foreignKey": "book._id" // 使用foreignKey表示,此字段关联book表的_id。
}, },
...@@ -366,8 +366,8 @@ schema保存至云端后,即可在前端直接查询。查询表设为order和 ...@@ -366,8 +366,8 @@ schema保存至云端后,即可在前端直接查询。查询表设为order和
// 客户端联表查询 // 客户端联表查询
const db = uniCloud.database() const db = uniCloud.database()
db.collection('order,book') // 注意collection方法内需要传入所有用到的表名,用逗号分隔,主表需要放在第一位 db.collection('order,book') // 注意collection方法内需要传入所有用到的表名,用逗号分隔,主表需要放在第一位
.where('book.title == "三国演义"') // 查询order表内书名为“三国演义”的订单 .where('book_id.title == "三国演义"') // 查询order表内书名为“三国演义”的订单
.field('book{title,author},quantity') // 这里联表查询book表返回book表内的title、book表内的author、order表内的quantity .field('book_id{title,author},quantity') // 这里联表查询book表返回book表内的title、book表内的author、order表内的quantity
.get() .get()
.then(res => { .then(res => {
console.log(res); console.log(res);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册