Read Data
Assume we have a collection todos containing records in the following format:
[
{
"_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...
]
Retrieve Data for a Record
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx"
});
// 1. Get the database reference
var db = app.database();
db.collection("todos")
.doc("todo-identifiant-aleatoire")
.get()
.then((res) => {
// res.data contains the data of this record
console.log(res.data);
});
// 1. Get the database reference
const db = wx.cloud.database();
db.collection("todos")
.doc("todo-identifiant-aleatoire")
.get()
.then((res) => {
// res.data contains the data of this record
console.log(res.data);
});
const cloudbase = require('@cloudbase/node-sdk')
const app = cloudbase.init({})
// 1. Get the database reference
const db = app.database()
exports.main = async (event, context) => {
const res = await db.collection('todos')
.doc('todo-identifiant-aleatoire')
.get()
return {
res
}
}
Due to database permissions, the client may fail to read data with the specified _id. Set the database permissions to only writable by the creator, readable by all or only writable by the admin side, readable by all. For details, refer to Database Permissions.
Retrieve Data for Multiple Records
We can also retrieve multiple records at once. By calling the where method on the collection to specify query conditions, then calling the get method, only records meeting the specified conditions will be returned. For example, to get all incomplete todo items. Sample code:
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx"
});
// 1. Get the database reference
var db = app.database();
db.collection("todos")
.where({
done: false
})
.get()
.then((res) => {
// res.data is an array containing the two records defined above
console.log(res.data);
});
// 1. Get the database reference
const db = wx.cloud.database();
db.collection("todos")
.where({
done: false
})
.get()
.then((res) => {
// res.data is an array containing the two records defined above
console.log(res.data);
});
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({});
// 1. Get the database reference
var db = app.database();
exports.main = async (event, context) => {
const res = await db
.collection("todos")
.where({
done: false
})
.get();
return {
res
};
};
The where method accepts an object parameter where each field and its value form a match condition that must be satisfied. The relationship between fields is "AND", meaning all conditions must be met simultaneously. In this example, it queries for records where done equals false.
Query Nested Fields
In query conditions, we can also specify matching the value of a nested field. For example, to find todo items marked as yellow:
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({
env: "xxxx"
});
// 1. Get the database reference
var db = app.database();
db.collection("todos")
.where({
style: {
color: "yellow"
}
})
.get()
.then((res) => {
console.log(res.data);
});
// 1. Get the database reference
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. Get the database reference
var db = app.database();
exports.main = async (event, context) => {
const res = await db.collection("todos")
.where({
style: {
color: "yellow"
}
})
.get()
return {
res
}
}
Use "dot notation" to represent nested fields:
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx"
});
// 1. Get the database reference
var db = app.database();
db.collection("todos")
.where({
"style.color": "yellow"
})
.get()
.then((res) => {
console.log(res.data);
});
// 1. Get the database reference
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. Get the database reference
const db = app.database();
exports.main = async (event, context) => {
const res = await db
.collection("todos")
.where({
"style.color": "yellow"
})
.get();
return {
res
};
};
Getting the Data of a Collection
To retrieve the data of a collection, for example, to get all records in the todos collection, you can call the get method on the collection. However, this is generally not recommended. On the client side, we should avoid retrieving excessive amounts of data at once and only fetch necessary data. To prevent misuse and protect user experience, when retrieving collection data, for requests from the Mini Program side, the server returns a maximum of 20 records by default; for requests from the Web side and Cloud Function side SDK, a maximum of 1000 records are returned, with a default of 100 records.
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx"
});
// 1. Get the database reference
var db = app.database();
db.collection("todos")
.get()
.then((res) => {
// res.data is an object containing all accessible records in the collection
console.log(res.data);
});
// 1. Get the database reference
const db = wx.cloud.database();
db.collection("todos")
.get()
.then((res) => {
// res.data is an object containing all accessible records in the collection
console.log(res.data);
});
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({});
// 1. Get the database reference
const db = app.database();
exports.main = async (event, context) => {
const res = await db.collection("todos").get();
return {
res
};
};