有奖捉虫:云通信与企业服务文档专题,速来> HOT
假设我们已有一个集合 todos,其中包含以下格式记录:
[
{
"_id": "todo-identifiant-aleatoire",
"due": Date("2018-09-01"),
"style": {
"color": "white"
},
"tags": ["cloud", "database"],
"process": 20,
"done": false
},
{
"_id": "todo-identifiant-aleatoire-2",
"due": Date("2018-12-25"),
"tags": ["cloud", "database"],
"style": {
"color": "yellow"
},
"process": 50,
"done": false
}
// more...
]

获取一个记录的数据

Web
小程序
Node.js
const cloudbase = require("@cloudbase/js-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. 获取数据库引用
var db = app.database();
db.collection("todos")
.doc("todo-identifiant-aleatoire")
.get()
.then((res) => {
// 2. 获取到返回数据后,在控制台打印查询得到的数据
// res.data 包含该记录的数据,本例中为上述 todos 集合上 id 为 todo-identifiant-aleatoire 的文档,即:
// [
// {
// "_id": "todo-identifiant-aleatoire",
// "due": Date("2018-09-01"),
// "style": {
// "color": "white"
// },
// "tags": ["cloud", "database"],
// "process": 20,
// "done": false
// }
// ]
console.log(res.data);
});
// 1. 获取数据库引用
const db = wx.cloud.database();
db.collection("todos")
.doc("todo-identifiant-aleatoire")
.get()
.then((res) => {
// 2. 获取到返回数据后,在控制台打印查询的到的数据
// res.data 包含该记录的数据,本例中为上述 todos 集合上 id 为 todo-identifiant-aleatoire 的文档,即:
// {
// "_id": "todo-identifiant-aleatoire",
// "due": Date("2018-09-01"),
// "style": {
// "color": "white"
// },
// "tags": ["cloud", "database"],
// "process": 20,
// "done": false
// }
console.log(res.data);
});

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({});
// 1. 获取数据库引用
const db = app.database();
exports.main = async (event, context) => {
const res = await db
.collection("todos")
.doc("todo-identifiant-aleatoire")
.get();

// 2. 获取到返回数据后,将数据返回
// res.data 包含该记录的数据,本例中为上述 todos 集合上 id 为 todo-identifiant-aleatoire 的文档,即:
// [
// {
// "_id": "todo-identifiant-aleatoire",
// "due": Date("2018-09-01"),
// "style": {
// "color": "white"
// },
// "tags": ["cloud", "database"],
// "process": 20,
// "done": false
// }
// ]
return {
res,
};
};

说明
由于数据库权限,用户端可能会读不到指定 _id 的数据,需要把数据库权限设定为 仅创建者可写,所有人可读仅管理端可写,所有人可读,具体可以参考 数据库权限

获取多个记录的数据

我们也可以一次性获取多条记录。通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,例如获取所有未完成的待办事项。示例代码如下:
Web
小程序
Node.js
const cloudbase = require("@cloudbase/js-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. 获取数据库引用
var db = app.database();

db.collection("todos")
.where({
done: false
})
.get()
.then((res) => {
// res.data 是包含以上定义的两条记录的数组
console.log(res.data);
});

// 1. 获取数据库引用
const db = wx.cloud.database();

db.collection("todos")
.where({
done: false
})
.get()
.then((res) => {
// res.data 是包含以上定义的两条记录的数组
console.log(res.data);
});

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({});
// 1. 获取数据库引用
var db = app.database();

exports.main = async (event, context) => {
const res = await db
.collection("todos")
.where({
done: false
})
.get();

return {
res
};
};

where 方法接收一个对象参数,该对象中每个字段和它的值构成一个需满足的匹配条件,各个字段间的关系是 的关系,即需同时满足这些匹配条件,在这个例子中,就是查询出 done 等于 false 的记录。

查询嵌套字段

在查询条件中我们也可以指定匹配一个嵌套字段的值,例如找出标为黄色的待办事项:
Web
小程序
Node.js
const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. 获取数据库引用
var db = app.database();

db.collection("todos")
.where({
style: {
color: "yellow"
}
})
.get()
.then((res) => {
console.log(res.data);
});

// 1. 获取数据库引用
const db = wx.cloud.database();

db.collection("todos")
.where({
style: {
color: "yellow"
}
})
.get()
.then((res) => {
console.log(res.data);
});

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({});
// 1. 获取数据库引用
var db = app.database();

exports.main = async (event, context) => {
const res = await db.collection("todos")
.where({
style: {
color: "yellow"
}
})
.get()

return {
res
}
}


使用点表示法表示嵌套字段:
Web
小程序
Node.js
const cloudbase = require("@cloudbase/js-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. 获取数据库引用
var db = app.database();

db.collection("todos")
.where({
"style.color": "yellow"
})
.get()
.then((res) => {
console.log(res.data);
});

// 1. 获取数据库引用
const db = wx.cloud.database();

db.collection("todos")
.where({
"style.color": "yellow"
})
.get()
.then((res) => {
console.log(res.data);
});

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({});
// 1. 获取数据库引用
const db = app.database();

exports.main = async (event, context) => {
const res = await db
.collection("todos")
.where({
"style.color": "yellow"
})
.get();

return {
res
};
};


获取一个集合的数据

如果要获取一个集合的数据,例如获取 todos 集合上的所有记录,可以在集合上调用 get 方法获取,但通常不建议这么使用,在客户端中我们需要尽量避免一次性获取过量的数据,只应获取必要的数据。为了防止误操作以及保护用户体验,在获取集合数据时,小程序端请求时,服务器默认且最多返回 20 条记录,Web 端及云函数端 SDK 请求时最多返回 1000 条,默认 100 条。
Web
小程序
Node.js
const cloudbase = require("@cloudbase/js-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. 获取数据库引用
var db = app.database();

db.collection("todos")
.get()
.then((res) => {
// res.data 是一个包含集合中有权限访问的所有记录的数据
console.log(res.data);
});

// 1. 获取数据库引用
const db = wx.cloud.database();

db.collection("todos")
.get()
.then((res) => {
// res.data 是一个包含集合中有权限访问的所有记录的数据
console.log(res.data);
});

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({});
// 1. 获取数据库引用
const db = app.database();

exports.main = async (event, context) => {
const res = await db.collection("todos").get();
return {
res
};
};