Skip to main content

Field Type

CloudBase document database supports multiple data types, meeting data storage requirements for diverse scenarios.

Basic Data Types​

Data TypeDescriptionExampleApplicable Scenarios
StringString type, used for storing text data"Hello World"username, description information, text content
NumberNumeric type, including integers and floating-point numbers123, 3.14age, price, counter
ObjectObject type, used for storing key-value pair structured data{ "name": "Zhang San", "age": 25 }user information, configuration data
ArrayArray type, used for storing ordered collections of data[1, 2, 3], ["a", "b", "c"]tag lists, product categories
BoolBoolean type, representing true or falsetrue, falseswitch states, permission flags
DateDate type, accurate to millisecondsnew Date()creation time, update time
GeoPointGeographical point, marking a location with latitude and longitude{ "latitude": 39.9, "longitude": 116.4 }location information, map markers
NullNull type, indicating the absence of a valuenulldefault 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: '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: '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 GeoPoint Related Documentation

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: 'Beijing Store',
location: location,
address: 'Chaoyang District, Beijing City'
}
}).then(res => {
console.log('Added successfully', res);
}).catch(err => {
console.error('Add failed', err);
});

Geospatial Query​

Mini Program GEO Query Documentation

const _ = db.command

db.collection('restaurants').where({
location: _.geoNear({
geometry: 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. ⚠️ Note: geoNear only supports the geometry, maxDistance, and minDistance options. It does not support limiting the number of returned records via an option (for example limit: 10). To limit the results, chain the limit() method after where(), for example: .where({ location: _.geoNear({ ... }) }).limit(10).get()