小程序入门学习08--云开发01

1 数组查询
1)新建带云开发功能的小程序
2)点击云开发->数据库->新建集合->添加几条记录

11863886-3bf94a954808a660
在这里插入图片描述

3)编码
index.wxml

<button class="b1" bindtap="query">数组查询</button>

index.json

{}

index.wxss

.b1{
  background-color: #eee;
}

index.js

/ 初始化数据库实例 command
const db = wx.cloud.database();
const _ = db.command
Page({
  query:function(){
    console.log("Query")
    //查询所有内容 获取并输出
    //db.collection("data").get().then(console.log)
    //查询count为(in)1 3 4 的数据 不为是nin
    db.collection("data")
    .where({
      count:_.nin([1,3,4])
    })
    .get().then(console.log)
  }
})

2 字段类型查询
为已有数据添加字段,然后

db.collection("data")
    //查询desc字段
      .field({
        desc:true
      })
      .get().then(console.log)

3 正则表达式

db.collection("data")
      .where({
        //new正则对象
        name: new db.RegExp({
          //正则字符串 匹配name-01 ... name-09
          regexp: 'name-0[1-9]',
          options: 'i'
        })
      })
      .get().then(console.log)

4 地理位置查询
index.wxml

<button class="b1" bindtap="add">新增地点</button>
<button class="b1" bindtap="query">字段查询</button>

index.js

const db = wx.cloud.database();
const _ = db.command
Page({
  query: function () {
    console.log("Query")
    //数据0的读取维度 ;不用res拿到的是一个数组,所以使用回调函数res方便后续处理
    db.collection('location').get().then(res=>{
      console.log(res.data[0].location.latitude)
    })
  },
  //添加地理位置索引
  add:function(){
    db.collection('location').add({
      //data表示需新增的json数据
      data:{
        location:db.Geo.Point(100.0012,10.0022)
      }
      //then() 异步执行 then前程序执行完后,执行then内部数据(一层一层剥开你的心)
    }).then(res=>{
      db.collection('location').add({
        data:{
          location:db.Geo.Point(101.0012,10.0022)
        }
      }).then(res=>{
        db.collection('location').add({
          data: {
            //创建一个点
            location: db.Geo.Point(101.0012, 10.0022)
          }
        })
      })
    })
  }
})

领取限量云产品优惠

发布了114 篇原创文章 · 获赞 32 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/moqianmoqian/article/details/104552184