From 067738e459a33b95c0aad740137783e0447b2dd6 Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Fri, 6 Mar 2020 22:57:46 +0800 Subject: [PATCH] Update cf-database.md --- docs/uniCloud/cf-database.md | 1788 ++++++++++++++++------------------ 1 file changed, 859 insertions(+), 929 deletions(-) diff --git a/docs/uniCloud/cf-database.md b/docs/uniCloud/cf-database.md index 22f7ed97e..7aad6e275 100644 --- a/docs/uniCloud/cf-database.md +++ b/docs/uniCloud/cf-database.md @@ -184,23 +184,20 @@ const collection = db.collection('user'); ```js // 单条插入数据 -collection.add({ +let res = await collection.add({ name: 'Ben' -}).then((res) => { - -}); +}) // 批量插入数据 -collection.add([{ +let res = await collection.add([{ name: 'Alex' },{ name: 'Ben' },{ name: 'John' -}]).then((res) => { +}]) // res.inserted // 插入成功条数 // res.result // 阿里云特有,批量插入返回的所有记录 id // res.failIndexes // 腾讯云特有,插入失败的记录的下标 -}); ``` **Tips** @@ -213,7 +210,7 @@ collection.add([{ 如果文档不存在,`set` 方法会创建一个新文档。 ```js -collection.doc().set({ +let res = await collection.doc().set({ name: "Hey" }); ``` @@ -235,12 +232,12 @@ collection.where() where 可接收对象作为参数,表示筛选出拥有和传入对象相同的 key-value 的文档。比如筛选出所有类型为计算机的、内存为 8g 的商品: ```js -db.collection('goods').where({ +let res = await db.collection('goods').where({ category: 'computer', type: { memory: 8, } -}) +}).get() ``` 如果要表达更复杂的查询,可使用高级查询指令,比如筛选出所有内存大于 8g 的计算机商品: @@ -267,14 +264,12 @@ collection.count() 参数 ```js -db.collection('goods').where({ +let res = await db.collection('goods').where({ category: 'computer', type: { memory: 8, } -}).count().then(function(res) { - -}) +}).count() ``` 响应参数 @@ -301,9 +296,7 @@ collection.limit() 使用示例 ```js -collection.limit(1).get().then(function(res) { - -}); +let res = await collection.limit(1).get() ``` ### 设置起始位置 @@ -319,9 +312,7 @@ collection.skip() 使用示例 ```js -collection.skip(4).get().then(function(res) { - -}); +let res = await collection.skip(4).get() ``` ### 对结果排序 @@ -338,9 +329,7 @@ collection.orderBy() 使用示例 ```js -collection.orderBy("name", "asc").get().then(function(res) { - -}); +let res = await collection.orderBy("name", "asc").get() ``` ### 指定返回字段 @@ -369,9 +358,9 @@ collection.field({ 'age': true }) ```js const myOpenID = "xxx" -db.collection('articles').where({ +let res = await db.collection('articles').where({ _openid: myOpenID -}) +}).get() ``` 还可以用指令: @@ -379,29 +368,29 @@ db.collection('articles').where({ ```js const dbCmd = db.command const myOpenID = "xxx" -db.collection('articles').where({ +let res = await db.collection('articles').where({ _openid: dbCmd.eq(openid) -}) +}).get() ``` 注意 `eq` 指令比对象的方式有更大的灵活性,可以用于表示字段等于某个对象的情况,比如: ```js // 这种写法表示匹配 stat.publishYear == 2018 且 stat.language == 'zh-CN' -db.collection('articles').where({ +let res = await db.collection('articles').where({ stat: { publishYear: 2018, language: 'zh-CN' } -}) +}).get() // 这种写法表示 stat 对象等于 { publishYear: 2018, language: 'zh-CN' } const dbCmd = db.command -db.collection('articles').where({ +let res = await db.collection('articles').where({ stat: dbCmd.eq({ publishYear: 2018, language: 'zh-CN' }) -}) +}).get() ``` #### neq @@ -412,12 +401,12 @@ db.collection('articles').where({ ```js const dbCmd = db.command -db.collection('goods').where({ +let res = await db.collection('goods').where({ category: 'computer', type: { brand: dbCmd.neq('X') }, -}) +}).get() ``` #### gt @@ -428,10 +417,10 @@ db.collection('goods').where({ ```js const dbCmd = db.command -db.collection('goods').where({ +let res = await db.collection('goods').where({ category: 'computer', price: dbCmd.gt(2000) -}) +}).get() ``` #### gte @@ -454,12 +443,12 @@ db.collection('goods').where({ ```js const dbCmd = db.command -db.collection('goods').where({ +let res = await db.collection('goods').where({ category: 'computer', type: { memory: dbCmd.in([8, 16]) } -}) +}).get() ``` #### nin @@ -580,16 +569,10 @@ collection.doc(_id).remove() ```js // 清理全部数据 -collection.get() - .then((res) => { - const promiseList = res.data.map(document => { - return collection.doc(document.id).remove(); - }); - Promise.all(promiseList); - }) - .catch((e) => { - - }); +let res = await collection.get() +res.data.map(async(document) => { + return await collection.doc(document.id).remove(); +}); ``` **方式2 条件查找文档然后直接批量删除** @@ -599,11 +582,9 @@ collection.where().remove() ```js // 删除字段a的值大于2的文档 const dbCmd = db.command -collection.where({ +let res = await collection.where({ a: dbCmd.gt(2) -}).remove().then(function(res) { - -}) +}).remove() ``` ## 更新文档 @@ -613,7 +594,7 @@ collection.where({ collection.doc().update() ```js -collection.doc('doc-id').update({ +let res = await collection.doc('doc-id').update({ name: "Hey", count: { fav: 1 @@ -645,15 +626,15 @@ collection.doc('doc-id').update({ 更新数组时,已数组下标作为key即可,比如以下示例将数组arr内下标为1的值修改为 uniCloud -``` -collection.doc('doc-id').update({ +```js +let res = await collection.doc('doc-id').update({ arr: { 1: "uniCloud" } }) ``` -``` +```json // 更新前 { "arr": ["hello", "world"] @@ -673,17 +654,15 @@ collection.doc().set() - 此方法会覆写已有字段,需注意与`update`表现不同,比如以下示例执行`set`之后`follow`字段会被删除 ```js -collection.doc('doc-id').set({ +let res = await collection.doc('doc-id').set({ name: "Hey", count: { fav: 1 } -}).then(function(res) { - -}); +}) ``` -``` +```json // 更新前 { "_id": "xxx", @@ -709,11 +688,9 @@ collection.update() ```js const dbCmd = db.command -collection.where({name: dbCmd.eq('hey')}).update({ +let res = await collection.where({name: dbCmd.eq('hey')}).update({ age: 18, -}).then(function(res) { - -}); +}) ``` @@ -727,13 +704,11 @@ collection.where({name: dbCmd.eq('hey')}).update({ ```js const dbCmd = db.command -db.collection('photo').doc('doc-id').update({ +let res = await db.collection('photo').doc('doc-id').update({ count: dbCmd.set({ fav: 1, follow: 1 }) -}).then(function(res) { - }) ``` @@ -773,14 +748,12 @@ db.collection('photo').doc('doc-id').update({ ```js const dbCmd = db.command -db.collection('user').where({ +let res = await db.collection('user').where({ _id: 'my-doc-id' }).update({ count: { fav: dbCmd.inc(1) } -}).then(function(res) { - }) ``` @@ -815,14 +788,12 @@ db.collection('user').where({ ```js const dbCmd = db.command -db.collection('user').where({ +let res = await db.collection('user').where({ _id: 'my-doc-id' }).update({ count: { fav: dbCmd.mul(10) } -}).then(function(res) { - }) ``` @@ -854,12 +825,9 @@ db.collection('user').where({ ```js const dbCmd = db.command -db.collection('comments').doc('comment-id').update({ +let res = await db.collection('comments').doc('comment-id').update({ rating: dbCmd.remove() -}).then(function(res) { - }) - ``` ``` @@ -883,16 +851,13 @@ db.collection('comments').doc('comment-id').update({ ```js const dbCmd = db.command -db.collection('comments').doc('comment-id').update({ +let res = await db.collection('comments').doc('comment-id').update({ // users: dbCmd.push('aaa') users: dbCmd.push(['c', 'd']) -}).then(function(res) { - }) - ``` -``` +```json // 更新前 { "_id": "xxx", @@ -912,14 +877,12 @@ db.collection('comments').doc('comment-id').update({ ```js const dbCmd = db.command -db.collection('comments').doc('comment-id').update({ +let res = await db.collection('comments').doc('comment-id').update({ users: dbCmd.pop() -}).then(function(res) { - }) ``` -``` +```json // 更新前 { "_id": "xxx", @@ -939,16 +902,13 @@ db.collection('comments').doc('comment-id').update({ ```js const dbCmd = db.command -db.collection('comments').doc('comment-id').update({ +let res = await db.collection('comments').doc('comment-id').update({ // users: dbCmd.push('aaa') users: dbCmd.unshift(['c', 'd']) -}).then(function(res) { - }) - ``` -``` +```json // 更新前 { "_id": "xxx", @@ -968,14 +928,12 @@ db.collection('comments').doc('comment-id').update({ ```js const dbCmd = db.command -db.collection('comments').doc('comment-id').update({ +let res = await db.collection('comments').doc('comment-id').update({ users: dbCmd.shift() -}).then(function(res) { - }) ``` -``` +```json // 更新前 { "_id": "xxx", @@ -1117,13 +1075,13 @@ interface IOptions { 示例: ```js -db.collection('user').where({ +let res = await db.collection('user').where({ location: db.command.geoNear({ geometry: new db.Geo.Point(lngA, latA), maxDistance: 1000, minDistance: 0 }) -}) +}).get() ``` #### geoWithin @@ -1154,11 +1112,11 @@ const area = new Polygon([ ]) // 搜索 location 字段在这个区域中的 user -db.collection('user').where({ +let res = await db.collection('user').where({ location: db.command.geoWithin({ geometry: area }) -}) +}).get() ``` #### geoIntersects @@ -1185,11 +1143,11 @@ const line = new LineString([ ]) // 搜索 location 与这条路径相交的 user -db.collection('user').where({ +let res = await db.collection('user').where({ location: db.command.geoIntersects({ geometry: line }) -}) +}).get() ```