Field Type
CloudBase document database supports multiple data types, meeting data storage requirements for diverse scenarios.
Basic Data Types
| Data Type | Description | Example | Applicable Scenarios |
|---|---|---|---|
| String | String type, used for storing text data | "Hello World" | username, description information, text content |
| Number | Numeric type, including integers and floating-point numbers | 123, 3.14 | age, price, counter |
| Object | Object type, used for storing key-value pair structured data | { "name": "Zhang San", "age": 25 } | user information, configuration data |
| Array | Array type, used for storing ordered collections of data | [1, 2, 3], ["a", "b", "c"] | tag lists, product categories |
| Bool | Boolean type, representing true or false | true, false | switch states, permission flags |
| Date | Date type, accurate to milliseconds | new Date() | creation time, update time |
| GeoPoint | Geographical point, marking a location with latitude and longitude | { "latitude": 39.9, "longitude": 116.4 } | location information, map markers |
| Null | Null type, indicating the absence of a value | null | default value for optional fields |
Detailed Explanation of Special Data Types
Date Type
Date type is used for storing time information with millisecond precision. It supports two creation methods: client time and server time.
Client Time
Using the built-in JavaScript Date object to create:
// Create current time.
const now = new Date();
// Create specified time.
const specificDate = new Date('2023-12-25 10:30:00');
// Insert data.
db.collection('articles').add({
data: {
title: title: 'Article title',
createTime: now,
publishTime: specificDate
}
});
Server Time
Use the serverDate object to create a server-side current time marker.
// Use server time
db.collection('articles').add({
data: {
title: title: 'Article title',
createTime: db.serverDate()
}
});
GeoPoint Geographic Location Type
``GeoPoint` is used to represent geographic points, uniquely marking a location via longitude and latitude coordinates.```
Implementations vary across SDKs, and the corresponding examples are shown below.
Creating a Geographic Location
- Mini Program
- Web
const db = wx.cloud.database();
// Create a geographic point
const location = db.Geo.Point(116.4, 39.9); // longitude, latitude
// Insert data with geographic locations.
db.collection('stores').add({
data: {
name: name: 'Beijing Store',
location: location,
address: address: 'Chaoyang District, Beijing City'
}
}).then(res => {
console.log('Added successfully', res);
}).catch(err => {
console.error('Add failed', err);
});
import cloudbase from '@cloudbase/js-sdk';
const app = cloudbase.init({
env: 'your-env-id'
});
const db = app.database();
// Create a geographic point
const location = new db.Geo.Point(116.4, 39.9); // longitude, latitude
// Insert data with geographic locations.
db.collection('stores').add({
name: name: 'Beijing Store',
location: location,
address: address: 'Chaoyang District, Beijing City'
}).then(res => {
console.log('Added successfully', res);
}).catch(err => {
console.error('Add failed', err);
});
Geospatial Query
- Mini Program
- Web
const _ = db.command
db.collection('restaurants').where({
location: _.geoNear({
geometry: db.Geo.Point(116.4, 39.9),
minDistance: 1000,
maxDistance: 5000,
})
}).get()
const _ = db.command
db.collection('restaurants').where({
location: _.geoNear({
geometry: new db.Geo.Point(116.4, 39.9),
minDistance: 1000,
maxDistance: 5000,
})
}).get()
⚠️ Note: When querying geospatial fields, create a geospatial index; otherwise the query will fail.
