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

docs: uniCloud database query

上级 c0b7ee3a
......@@ -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"
}]
}]
}
```
## 删除文档
......
......@@ -281,7 +281,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.
先完成此消息的编辑!
想要评论请 注册