Query data.
Initialize SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID.
});
const db = app.database();
const _ = db.command; // Get query command
Single query
Query specified records by document ID.
db.collection(collectionName).doc(docId).get()
- collectionName: Collection Name
- docId: Document ID
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| docId | string | Yes | Unique identifier of the document |
Sample Code
Query a single record by document ID
const result = await db.collection('todos')
.doc('docId')
.get()
Returned result
{
data: [{
_id: "todo-id-123",
title: "Learn CloudBase"
completed: false,
// ... Other Fields
}],
}
Multiple queries
Query multiple records within the collection, supporting conditional filtering, sorting, and pagination.
db.collection(collectionName).where(conditions).get()
- collectionName: Collection Name
- conditions: Query conditions (optional)
⚠️ Note: The get() method returns 100 data entries by default. If needed, please use pagination.
Parameter Description
| Method | Parameter Type | Required | Description |
|---|---|---|---|
| where() | object | No | Query condition, supported operators |
| orderBy() | string, string | No | Sorting field and direction ('asc' or 'desc') |
| limit() | number | No | Limit the number of returned records, default 100, maximum 1000 |
| skip() | number | No | Number of records to skip, for pagination |
| field() | object | No | Specify the return field. true means return, false means do not return |
Query all records
const result = await db.collection('todos').get()
// Conditional query
const result = await db.collection('todos')
.where({
completed: false,
priority: 'high'
})
.get()
Complex Query
const _ = db.command
// Complex conditional query
const result = await db.collection('todos')
.where({
// age above 18
age: _.gt(18),
// Tag contains 'technology'
tags: _.in(['technology', 'study']),
createTime in the past week
createdAt: _.gte(new Date(Date.now() - 7 * 24 * 60 * 60 * 1000))
})
.orderBy('createdAt', 'desc') // Sort by creation time in reverse chronological order
.limit(10) // Limit to 10 records
.skip(0) // Skip 0 records (page)
.field({ // Only return specified field
title: true,
completed: true,
createdAt: true
})
.get()
Paging Query
// Paging Query Example
const pageSize = 10
const pageNum = 1
const result = await db.collection('todos')
.orderBy('createdAt', 'desc')
.skip((pageNum - 1) * pageSize)
.limit(pageSize)
.get()
Aggregate Query
Refer to Aggregate Query for aggregation grammar
//Statistical Query
const result = await db.collection('todos')
.aggregate()
.group({
_id: '$priority',
count: {
$sum: 1
}
})
.end()
console.log('Statistics by priority:', result.list)
Geolocation query
⚠️ Note: When querying a geolocation field, create a geo-indexing, otherwise the query will fail.
const _ = db.command
// Query nearby users (sort by distance)
db.collection("users").where({
location: _.geoNear({
geometry: new db.Geo.Point(116.404, 39.915), // Tiananmen coordinate
maxDistance: 1000, // Maximum distance 1000 meters
minDistance: 0 // Minimum distance 0 meters
})
}).get()
// Query users in the specified region
db.collection("users").where({
location: _.geoWithin({
geometry: polygon // Use the polygon defined above
})
}).get()
// Query users intersecting with the path
db.collection("users").where({
location: _.geoIntersects({
geometry: line // Use the path defined above
})
}).get()