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

docs: uniCloud db

上级 17bb3cd2
...@@ -1536,7 +1536,7 @@ db.runTransaction(callback: function, times: number) ...@@ -1536,7 +1536,7 @@ db.runTransaction(callback: function, times: number)
```javascript ```javascript
const db = uniCloud.database() const db = uniCloud.database()
const _ = db.command const dbCmd = db.command
exports.main = async (event) => { exports.main = async (event) => {
try { try {
const result = await db.runTransaction(async transaction => { const result = await db.runTransaction(async transaction => {
...@@ -1546,11 +1546,11 @@ exports.main = async (event) => { ...@@ -1546,11 +1546,11 @@ exports.main = async (event) => {
if (aaaRes.data && bbbRes.data) { if (aaaRes.data && bbbRes.data) {
try { try {
const updateAAARes = await transaction.collection('account').doc('aaa').update({ const updateAAARes = await transaction.collection('account').doc('aaa').update({
amount: _.inc(-10) amount: dbCmd.inc(-10)
}) })
const updateBBBRes = await transaction.collection('account').doc('bbb').update({ const updateBBBRes = await transaction.collection('account').doc('bbb').update({
amount: _.inc(10) amount: dbCmd.inc(10)
}) })
console.log(`transaction succeeded`) console.log(`transaction succeeded`)
...@@ -1609,7 +1609,7 @@ db.startTransaction() ...@@ -1609,7 +1609,7 @@ db.startTransaction()
```javascript ```javascript
const db = uniCloud.database() const db = uniCloud.database()
const _ = db.command const dbCmd = db.command
exports.main = async (event) => { exports.main = async (event) => {
const transaction = await db.startTransaction() const transaction = await db.startTransaction()
...@@ -1620,11 +1620,11 @@ exports.main = async (event) => { ...@@ -1620,11 +1620,11 @@ exports.main = async (event) => {
if (aaaRes.data && bbbRes.data) { if (aaaRes.data && bbbRes.data) {
const updateAAARes = await transaction.collection('account').doc('aaa').update({ const updateAAARes = await transaction.collection('account').doc('aaa').update({
amount: _.inc(-10) amount: dbCmd.inc(-10)
}) })
const updateBBBRes = await transaction.collection('account').doc('bbb').update({ const updateBBBRes = await transaction.collection('account').doc('bbb').update({
amount: _.inc(10) amount: dbCmd.inc(10)
}) })
await transaction.commit() await transaction.commit()
...@@ -2837,8 +2837,9 @@ let res = await db.collection('orders').aggregate() ...@@ -2837,8 +2837,9 @@ let res = await db.collection('orders').aggregate()
- orders 的 book 字段与 books 的 title 字段相等 - orders 的 book 字段与 books 的 title 字段相等
- books 的 stock 字段 大于或等于 orders 的 quantityorders 字段 - books 的 stock 字段 大于或等于 orders 的 quantityorders 字段
```js ```js
const db = cloud.database() const db = cloud.database()
const $ = db.command.aggregate const dbCmd = db.command
const $ = dbCmd.aggregate
let res = await db.collection('orders').aggregate() let res = await db.collection('orders').aggregate()
.lookup({ .lookup({
from: 'books', from: 'books',
...@@ -2847,7 +2848,7 @@ let res = await db.collection('orders').aggregate() ...@@ -2847,7 +2848,7 @@ let res = await db.collection('orders').aggregate()
order_quantity: '$quantity' order_quantity: '$quantity'
}, },
pipeline: $.pipeline() pipeline: $.pipeline()
.match(_.expr($.and([ .match(dbCmd.expr($.and([
$.eq(['$title', '$$order_book']), $.eq(['$title', '$$order_book']),
$.gte(['$stock', '$$order_quantity']) $.gte(['$stock', '$$order_quantity'])
]))) ])))
...@@ -2993,9 +2994,9 @@ match({ ...@@ -2993,9 +2994,9 @@ match({
``` ```
```js ```js
// 使用操作符 // 使用操作符
const _ = db.command const dbCmd = db.command
match({ match({
age: _.gt(18) age: dbCmd.gt(18)
}) })
``` ```
...@@ -3035,12 +3036,12 @@ match 过滤出文档后,还可以与其他流水线阶段配合使用。 ...@@ -3035,12 +3036,12 @@ match 过滤出文档后,还可以与其他流水线阶段配合使用。
比如下面这个例子,我们使用 group 进行搭配,计算 score 字段大于 80 的文档数量: 比如下面这个例子,我们使用 group 进行搭配,计算 score 字段大于 80 的文档数量:
```js ```js
const _ = db.command const dbCmd = db.command
const $ = _.aggregate const $ = dbCmd.aggregate
let res = await db.collection('articles') let res = await db.collection('articles')
.aggregate() .aggregate()
.match({ .match({
score: _.gt(80) score: dbCmd.gt(80)
}) })
.group({ .group({
_id: null, _id: null,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册