提交 98abe254 编写于 作者: study夏羽's avatar study夏羽

Merge branch 'master' of https://github.com/dcloudio/uni-app

......@@ -892,6 +892,131 @@ db.collection('articles').where({
})
})
```
### 查询数组字段@querywitharr
假设数据表class内有以下数据,可以使用下面两种方式查询数组内包含指定值
```js
{
"_id": "1",
"students": ["li","wang"]
}
{
"_id": "2",
"students": ["wang","li"]
}
{
"_id": "3",
"students": ["zhao","qian"]
}
```
#### 指定下标查询
```js
const index = 1
const res = await db.collection('class').where({
['students.' + index]: 'wang'
})
.get()
```
```js
// 查询结果如下
{
data: [{
"_id": "1",
"students": ["li","wang"]
}]
}
```
#### 不指定下标查询
```js
const res = await db.collection('class').where({
students: 'wang'
})
.get()
```
查询结果如下
```js
{
data: [{
"_id": "1",
"students": ["li","wang"]
},{
"_id": "1",
"students": ["wang","li"]
}]
}
```
#### 数组内是对象
如果将上面class内的数据改为如下形式
```js
{
"_id": "1",
"students": [{
name: "li"
},{
name: "wang"
}]
}
{
"_id": "2",
"students": [{
name: "wang"
},{
name: "li"
}]
}
{
"_id": "3",
"students": [{
name: "zhao"
},{
name: "qian"
}]
}
```
不指定下标查询的写法可以修改为
```js
const res = await db.collection('class').where({
'students.name': 'wang'
})
.get()
```
查询结果如下
```js
{
data: [{
"_id": "1",
"students": [{
name: "li"
},{
name: "wang"
}]
},
{
"_id": "2",
"students": [{
name: "wang"
},{
name: "li"
}]
}]
}
```
## 删除文档
......
......@@ -270,6 +270,26 @@ sql写法,对js工程师而言有学习成本,而且无法处理非关系型
**注意编写查询条件时,除test外,均为运算符左侧为数据库字段,右侧为常量**
#### 查询数组字段@querywitharr
如果数据库存在以下记录
```js
{
"_id": "1",
"students": ["li","wang"]
}
{
"_id": "2",
"students": ["wang","li"]
}
{
"_id": "3",
"students": ["zhao","qian"]
}
```
使用jql查询语法时,可以直接使用`student=='wang'`作为查询条件来查询students内包含wang的记录。
#### 常见正则用法@regexp
......@@ -281,7 +301,7 @@ sql写法,对js工程师而言有学习成本,而且无法处理非关系型
<template>
<view class="content">
<input @input="onKeyInput" placeholder="请输入搜索值" />
<unicloud-db v-slot:default="{data, loading, error, options}" collection="goods" :where=`/${searchVal}/i.test(name)`>
<unicloud-db v-slot:default="{data, loading, error, options}" collection="goods" :where=`${new RegExp(searchVal, 'i')}.test(name)`>
<view v-if="error">{{error.message}}</view>
<view v-else>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册