数据云API

REST API 详解

对象

用户

角色

ACL

批量操作

文件(file)

更新操作符

查询

错误代码清单

提示


现有5张系统表:accessToken、file、role、roleMapping、user

为了显示跟自定义表进行区别,所以统一在开始位置了加下划线以示区别,_accessToken、_file、_role、_roleMapping、_user。实际操作不需要加下划线。

REST API 详解

Restful API可以让您用任何可以发送 http 请求的设备来与 API Cloud 进行交互, 您可以使用 Restful API 做以下事情, 例如:

  • 一个移动网站可以通过 Javascript 来获取 API Cloud 上的数据.
  • 一个网站可以展示来自 API Cloud 的数据。
  • 您可以上传大量的数据, 之后可以被一个移动 app 读取。
  • 使用任何语言写的程序都可以操作 API Cloud 上的数据。
  • 如果您不再需要使用 API Cloud,您可以导出您所有的数据。

API 版本

  • 1.0 版本:2014年9月15日发布。

快速参考

所有的API访问都是通过HTTP进行的。相关API访问需要在 https://d.apicloud.com/ 下。

对象

URLHTTP功能
/mcm/api/<className>POST创建对象
/mcm/api/<className>/<objectId>GET获取对象
/mcm/api/<className>/<objectId>PUT更新对象
/mcm/api/<className>GET查询对象
/mcm/api/<className>/<objectId>DELETE删除对象
/mcm/api/<className>/countGET统计对象数量
/mcm/api/<className>/<objectId>/existsGET判断对象是否存在

Relation对象

/mcm/api/<className>/<objectId>/<relationColName>GET获取关联对象
/mcm/api/<className>/<objectId>/<relationColName>POST创建关联对象
/mcm/api/<className>/<objectId>/<relationColName>/countGET统计关联对象数量
/mcm/api/<className>/<objectId>/<relationColName>DELETE删除所有关联对象

用户

URLHTTP功能
/mcm/api/user/POST新增用户
/mcm/api/user/loginPOST登录
/mcm/api/user/logoutPOST登出
/mcm/api/user/verifyEmailPOST发送验证邮件
/mcm/api/user/resetRequestPOST密码重置
/mcm/api/user/<objectId>GET获取用户
/mcm/api/user/<objectId>PUT更改用户信息
/mcm/api/user/<objectId>DELETE删除用户

角色

URLHTTP功能
/mcm/api/rolePOST创建角色
/mcm/api/role/<objectId>GET获取角色
/mcm/api/role/<objectId>PUT更新角色
/mcm/api/role/<objectId>DELETE删除角色

请求验证

当调用 APICloud 云开发接口时,我们需要对头部信息中X-APICloud-AppKey 进行验证,X-APICloud-AppKey 的生成规则如下:

  1. var appKey = SHA1(应用ID + 'UZ' + 应用KEY +'UZ' + 当前时间毫秒数)+ '.' +当前时间毫秒数

例如:您的应用ID是A6968565094002,而您的应用KEY是62FB16B2-0ED6-B460-1F60-EB61954C823B,则您在请求头部信息X-APICloud-AppKey中设置的值应为 “A6968565094002”+”UZ”+”62FB16B2-0ED6-B460-1F60-EB61954C823B”+”UZ”+当前时间毫秒数组合字符串后通过SHA1加密后,返回的字符串+.当前时间毫秒数。

示例代码如下:

  1. var now = Date.now();
  2. var appKey = SHA1("A6968565094002"+"UZ"+"62FB16B2-0ED6-B460-1F60-EB61954C823B"+"UZ"+now)+"."+now

SHA1下载地址

请求格式

对于POST和PUT请求,请求的主体必须是 JSON 格式,而且 HTTP header 的 Content-Type 需要设置为 application/json。 用户验证是通过 HTTP header 来进行的。X-APICloud-AppId头标明正在运行的是哪个App程序,而X-APICloud-AppKey头用来授权鉴定终端。 对于 Javascript 使用,API Cloud 支持跨域资源共享,所以您可以将这些 header 同 XMLHttpRequest 一同使用。

响应格式

对于所有的请求的响应格式都是一个 JSON 对象。 一个请求是否成功是由 HTTP 状态码标明的。一个 2XX 的状态码表示成功,而一个 4XX 表示请求失败。当一个请求失败时响应的主体仍然是一个 JSON 对象,但是总是会包含 code。 您可以用它们来进行调试。

对象

对象格式

通过 REST API 保存数据需要将对象的数据通过 JSON 来编码。 这个数据是无模式化的(Schema Free),这意味着您不需要提前标注每个对象上有那些 key。您只需要随意设置 key-value 对就可以, 后端会存储它的。举个例子,假设您准备设置一个公司信息。一个简单的对象可能包含:

  1. {
  2. "name": "API Cloud",
  3. "level": "Branch",
  4. "area": "Haidian District"
  5. }

Key 必须是字母和数字组成的 String,Value 可以是任何可以 JSON 编码的东西。每个对象都有一个类名,您可以通过类名来区分不同的数据。例如, 我们可以把公司对象称之为 Company。我们推荐您使用 NameYourClassesLikeThis 和 nameYourKeysLikeThis 这样的格式为您的 Key-Value 命名,可以使您的代码看起来很漂亮。当您从 API Cloud 中获取对象时,一些字段会被自动加上:createdAt,updatedAt 和 objectID。这些字段的名字是保留的,您不能自行设置它们。我们上面设置的对象在获取时应该是下面的样子。

  1. {
  2. "name": "API Cloud",
  3. "level": "Branch",
  4. "area": "Haidian District",
  5. "createAt": "2014-08-20T02:06:57.931Z",
  6. "updateAt": "2014-08-20T02:06:57.931Z ",
  7. "objectId": "5436219ea1a14d1c60de3e05"
  8. }

createdAt 和 updatedAt 都是 UTC 时间戳,以 ISO 8601 标准和毫秒级精度储存:YYYY-MM-DDTHH:MM:SS.MMMMZ。objectId 是一个 string,在类中唯一标明了一个对象。在 REST API 中 class 级的在一个资源上的操作只会根据类名来进行。例如,如果类名是 Company,那么 class 的 URL 就是 http://d.apicloud.com/mcm/api/Company

针对于一个特定的对象的操作可以通过组织一个 URL 来做。例如, 对 Company 中的一个 objectId 为 5436219ea1a14d1c60de3e05 的对象的操作应使用如下 URL: https://d.apicloud.com/mcm/api/Company/5436219ea1a14d1c60de3e05

创建对象

为了在 API Cloud 上创建一个新的对象,应该向 class 的 URL 发送一个 POST 请求,其中应该包含对象本身。例如,要创建如上对象:

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"name": "API Cloud","level": "Branch","area": "Haidian District"}' \
  6. https://d.apicloud.com/mcm/api/Company

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({
  4. "name": "API Cloud",
  5. "level": "Branch",
  6. "area": "Haidian District"
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {
  10. "name": "API Cloud",
  11. "level": "Branch",
  12. "area": "Haidian District"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

当创建成功时,HTTP的返回Code是 200,响应的主体是一个 JSON 对象,包含新的对象的 objectId,createdAt和updateAt 时间戳。

  1. {
  2. "id": "5436442ca1a14d1c60de3e06",
  3. "name": "API Cloud",
  4. "level": "Branch",
  5. "area": "Haidian District",
  6. "createdAt": "2014-10-09T08:15:40.843Z",
  7. "updatedAt": "2014-10-09T08:15:40.843Z"
  8. }

获取对象

当您创建了一个对象时,您可以通过发送一个 GET 请求以获取它的内容。例如,为了得到我们上面创建的对象: curl

  1. curl -X GET \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. https://d.apicloud.com/mcm/api/Company/5436442ca1a14d1c60de3e06

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.get({"_id":"5436442ca1a14d1c60de3e06"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/5436442ca1a14d1c60de3e06",
  3. "type": "GET",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. }
  9. }).done(function (data, status, header) {
  10. //success body
  11. }).fail(function (header, status, errorThrown) {
  12. //fail body
  13. })

返回的主体是一个 JSON 对象包含所有用户提供的字段加上 createdAt,updatedAt 和 objectId 字段:

  1. {
  2. "id": "5436442ca1a14d1c60de3e06",
  3. "name": "API Cloud",
  4. "level": "Branch",
  5. "area": "Haidian District",
  6. "createdAt": "2014-10-09T08:15:40.843Z",
  7. "updatedAt": "2014-10-09T08:15:40.843Z"
  8. }

更改对象

为了更改一个对象上已经有的数据,您可以发送一个 PUT 请求到对象相应的 URL 上,任何您未指定的 key 都不会更改,所以您可以只更新对象数据的一个子集。例如,我们来更改我们对象的一个 area 的字段:

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"area":"Dongcheng District"}' \
  6. https://d.apicloud.com/mcm/api/Company/5436442ca1a14d1c60de3e06

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"5436442ca1a14d1c60de3e06"},{"area":"Dongcheng District"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/5436442ca1a14d1c60de3e06",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"area":"Dongcheng District","_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

返回的主体是一个 JSON 对象包含所有用户提供的字段加上 createdAt,updatedAt 和 objectId 字段,其中 updatedAt 为最新的UTC更新时间戳。

  1. {
  2. "id": "5436442ca1a14d1c60de3e06",
  3. "name": "API Cloud",
  4. "level": "Branch",
  5. "area": "Dongcheng District",
  6. "createdAt": "2014-10-09T08:15:40.843Z",
  7. "updatedAt": "2014-10-09T08:34:28.153Z"
  8. }

删除对象

为了在 API Cloud 中删除一个对象,可以发送一个 DELETE 请求到指定的对象的 URL,比如:

curl

  1. curl -X DELETE \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. https://d.apicloud.com/mcm/api/Company/5436442ca1a14d1c60de3e06

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.delete({"_id":"5436442ca1a14d1c60de3e06"},function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/5436442ca1a14d1c60de3e06",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"_method":"DELETE"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

数据类型

在API Cloud中创建对象时,相关对象字段数据类型可以被定义为以下十种的任何一种,相关支持数据类型如下:

  • String (字符串)
  • Number (数字)
  • Boolean (布尔值)
  • Date (日期时间)
  • File (文件类型)包含id、url、name三个字段
  • Array (数组)
  • Object (对象)与JSON对象相同
  • GeoPoint (地理位置)包含lat、lng两个字段
  • Pointer (一对一)
  • Relation (一对多)

用户

新增用户

默认everyone权限

注册一个新用户时 username 、password两个字段都是必要的。password 字段会以和其他的字段不一样的方式处理,它在储存时会被加密而且永远不会被返回给任何来自客户端的请求。 当新增一个用户,需要向 user 路径发送一个 POST 请求,示例如下:

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"username":"apicloud","password":"123456","email":"test@apicloud.com"}' \
  6. https://d.apicloud.com/mcm/api/user

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.save({
  4. "username":"apicloud",
  5. "password":"123456",
  6. "email":"test@apicloud.com"
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data":{
  10. "username":"apicloud",
  11. "password":"123456",
  12. "email":"test@apicloud.com"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

如果创建成功,返回的状态码是200,返回的主体是一个 JSON 对象,包含 objectId, createdAt和updatedAt 等属性信息,示例如下:

  1. {
  2. "createdAt": "2014-10-14T07:16:29.248Z",
  3. "updatedAt": "2014-10-14T07:16:29.248Z",
  4. "id": "5436442ca1a14d1c60de3e16",
  5. "username": "apicloud",
  6. "email": "test@apicloud.com"
  7. }

验证Email

设置 Email验证是API Cloud云设置选项中的一项。Email 验证会在 User 对象中改变 emailVerified 字段的值, 当一个用户的 Email 被新设置或者修改过的话,emailVerified 会被设定为 false。 API Cloud 会对用户填写的邮箱发送一个链接,当用户点击这个链接时,可以把 emailVerified 设置为 true。

emailVerified 字段有三种状态:

  • true : 用户点击 email 中的地址来连接API Cloud 来验证Email地址。邮箱验证通过后,emailVerified 为 true。
  • false : User 对象最后一次被刷新的时候,用户并没有确认过他的 Email 地址,如果您看到 emailVerified 为 false 的话,您可以考虑刷新 User 对象。
  • null : 所创建User 对象未输入email。

请求验证Email

默认everyone权限

发送给用户的邮箱验证邮件在两周内失效,为了发送验证请求,需要向verifyEmail路径发送一个 POST 请求,示例如下:

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"username":"apicloud","email":"customer@mail.apicloud.com","language":"zh_CN"}' \
  6. https://d.apicloud.com/mcm/api/user/verifyEmail

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.verify({
  4. "username":"apicloud",
  5. "email":"customer@mail.apicloud.com",
  6. "language":"zh_CN"
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/verifyEmail",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {
  10. "username":"apicloud",
  11. "email":"customer@mail.apicloud.com",
  12. "language":"zh_CN"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

无论发送邮件成功或失败,返回的主体都是JSON对象,成功返回:

  1. {
  2. "status": 1,
  3. "msg": "发送验证邮件成功"
  4. }

否则,如果失败,将返回包含具体错误原因信息的JSON对象:

  1. {
  2. "status": 0,
  3. "code": 119,
  4. "msg": "请在设置中开启邮箱验证服务"
  5. }

注意:在进行邮件发送前,请先确认已经设置了云设置中的“注册用户邮箱验证”状态为开启状态。

密码重置

默认everyone权限

您可以使用这项功能,前提是用户将 Email 与他们的账户关联起来。如果执行重置密码操作,需要发送一个 POST 请求到 /resetRequest,示例如下:

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"username":"apicloud","email":"test@mail.apicloud.com","language":"zh_CN"}' \
  6. https://d.apicloud.com/mcm/api/user/resetRequest

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.reset({
  4. "username":"apicloud",
  5. "email":"customer@mail.apicloud.com",
  6. "language":"zh_CN"
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/resetRequest",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {
  10. "username":"apicloud",
  11. "email":"customer@mail.apicloud.com",
  12. "language":"zh_CN"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

如果发送成功,返回的主体是一个 JSON 对象:

  1. {
  2. "msg": "请到邮箱查收邮件"
  3. }

获取用户

默认owner权限

您可以发送一个 GET 请求到 URL 以获取用户信息,示例如下:

curl

  1. curl -X GET \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "authorization":{{login 返回的id}}" \
  5. -H "Content-Type: application/json" \
  6. https://d.apicloud.com/mcm/api/user/5437a1a9e41cbf4a52d7c9d6

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.get({"_id":"5437a1a9e41cbf4a52d7c9d6"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/5437a1a9e41cbf4a52d7c9d6",
  3. "type": "GET",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. }
  9. }).done(function (data, status, header) {
  10. //success body
  11. }).fail(function (header, status, errorThrown) {
  12. //fail body
  13. })

返回的 body 是一个 JSON 对象,包含所有用户提供的字段,除了密码以外,也包括了 createdAt、updatedAt 和 objectId 字段。

  1. {
  2. "createdAt": "2014-10-14T07:16:29.248Z",
  3. "updatedAt": "2014-10-14T07:25:52.026Z",
  4. "id": "5437a1a9e41cbf4a52d7c9d6",
  5. "username": "apicloud",
  6. "email": "test@mail.apicloud.com",
  7. "emailVerified": null,
  8. "verificationToken": "701fc3d75900a0307f30e084e8b24136f9784b05f8f21ed23b472453912e9604ee8561d3d10167cb10d36f3ea9c1b5c01cc2d43242ace893ab5902637e45d6e9"
  9. }

注意: 在进行用户获取操作后,相关系统会返回一个verificationToken(类似于userToken),相关Token时效为15分钟。

更新用户

默认owner权限

为了更改一个用户已有的信息,需要对这个用户的 URL 发送一个 PUT 请求。 任何您没有指定过的 key 会保持不动,所以您可以只改动用户数据中的一部分,username 和 password 均可以改动,您可以发送一个 PUT 请求到 URL 以更改用户信息,示例如下:

如果我们想对用户居住地址的信息做出一些修改:

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "authorization":{{login 返回的id}}" \
  5. -H "Content-Type: application/json" \
  6. -d '{"address":"No.10, Building 3, Haiwei road, Haidian district"}' \
  7. https://d.apicloud.com/mcm/api/user/543ccdcd6c0a61303282414e

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.save({"_id":"543ccdcd6c0a61303282414e"},{"address":"No.10, Building 3, Haiwei road, Haidian district"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/543ccdcd6c0a61303282414e",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"address":"No.10, Building 3, Haiwei road, Haidian district","_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

如果成功,返回的 body 是一个 JSON 对象:

  1. {
  2. "createdAt": "2014-10-14T07:16:29.248Z",
  3. "updatedAt": "2014-10-14T07:52:34.055Z",
  4. "id": "543ccdcd6c0a61303282414e",
  5. "username": "apicloud",
  6. "email": "test@mail.apicloud.com",
  7. "emailVerified": null,
  8. "verificationToken": "701fc3d75900a0307f30e084e8b24136f9784b05f8f21ed23b472453912e9604ee8561d3d10167cb10d36f3ea9c1b5c01cc2d43242ace893ab5902637e45d6e9",
  9. "address": "No.10, Building 3, Haiweiroad, Haidian district"
  10. }

删除用户

默认owner权限

为了在 API Cloud 上删除一个用户,可以向它的 URL 上发送一个 DELETE 请求,示例如下:

curl

  1. curl -X DELETE \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "authorization":{{login 返回的id}}" \
  5. -H "Content-Type: application/json" \
  6. https://d.apicloud.com/mcm/api/user/5437a1a9e41cbf4a52d7c9d6

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.delete({"_id":"5437a1a9e41cbf4a52d7c9d6"},function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/5437a1a9e41cbf4a52d7c9d6",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"_method":"DELETE"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

用户登录

默认erveyone权限

在您允许用户注册之后, 在以后您需要让他们用自己的用户名和密码登陆. 为了做到这一点, 发送一个 POST 请求到 /mcm/api/user/login, 加上 username 和 password 作为参数.

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"username":"apicloud","password":"111111"}' \
  6. https://d.apicloud.com/mcm/api/user/login

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.login({"username":"apicloud","password":"111111"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/login",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"username":"apicloud","password":"111111"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

返回的主体是一个 JSON 对象. 它包含了 createdAt,updateAt,id,userId 和 ttl 字段.

  1. {
  2. "createdAt": "2015-02-02T11:12:18.484Z",
  3. "updatedAt": "2015-02-02T11:12:18.484Z",
  4. "id": "Ta48prapSvEuwGzgPuM9c8TtbHKJV0mfh4nqcVqEgpAVhMrCz2m1ih6lRZ04V73J",
  5. "ttl": 1209600,
  6. "userId": "54cf5b7bd41a2a720ee3d5fd"
  7. }

退出登录

默认erveyone权限

登录完成后可以把返回的id作为token进行请求,如果需要注销用户,需要发送一个 POST 请求到 /mcm/api/user/logout,把login返回的id作为headers里authorization的参数

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "authorization":{{login 返回的id}}" \
  5. -H "Content-Type: application/json" \
  6. https://d.apicloud.com/mcm/api/user/logout

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("user");
  3. Model.logout({token: "{{login 返回的id}}"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/user/logout",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}",
  8. "authorization":{{login 返回的id}}"
  9. }
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

如果成功,返回的主体是一个 空JSON 对象,

  1. {}

角色

当您的 app 的规模和用户基数成长时,您可能发现您需要比 ACL 模型 (针对每个用户) 更加粗粒度的访问控制您的数据的方法。为了适应这种需求, API Cloud支持一种基于角色的权限控制方式。角色系统提供一种逻辑方法让您通过权限的方式来访问您的API Cloud 数据。角色是一种有名称的对象,包含了用户和其他角色。任何授予一个角色的权限隐含着授予它包含着的其他的角色相应的权限。

创建角色

创建一个新角色,需要发送一个 POST 请求到 role 根路径,相关示例如下:

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{"name":"manager1","description":"manager desc"}' \
  6. https://d.apicloud.com/mcm/api/role

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("role");
  3. Model.save({"name":"manager1","description":"manager desc"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/role",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"name":"manager1","description":"manager desc"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

如果创建成功,返回的 body 是一个 JSON 对象,除了包括角色名称外也包括了 createdAt、updatedAt 和 objectId 字段,相关JSON返回对象如下:

  1. {
  2. "createdAt": "2014-10-16T02:31:42.399Z",
  3. "updatedAt": "2014-10-16T02:31:42.399Z",
  4. "id": "543f2e0e474f229d61185565",
  5. "name": "manager",
  6. "description": "manager desc"
  7. }

获取角色

您可以同样通过发送一个 GET 请求来获取某个角色对象。例如我们想要获取上面创建的对象:

curl

  1. curl -X GET \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. https://d.apicloud.com/mcm/api/role/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("role");
  3. Model.get({"_id":"543f2e0e474f229d61185565"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/role/543f2e0e474f229d61185565",
  3. "type": "GET",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. }
  9. }).done(function (data, status, header) {
  10. //success body
  11. }).fail(function (header, status, errorThrown) {
  12. //fail body
  13. })

如果成功,响应的 body 是一个包含角色所有字段的JSON对象:

  1. {
  2. "createdAt": "2014-10-16T02:31:42.399Z",
  3. "updatedAt": "2014-10-16T02:31:42.399Z",
  4. "id": "543f2e0e474f229d61185565",
  5. "name": "manager",
  6. "description": "manager desc"
  7. }

更新角色

您可以同样通过发送一个 PUT 请求来更新某个角色对象,比如我们想要更新上面所创建角色对象的描述:

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"name":"manager1"}' \
  5. https://d.apicloud.com/mcm/api/role/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("role");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"name":"manager1"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/role/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"name":"manager1","_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

响应的 body 是一个包含角色的所有字段(含更新字段)的JSON对象:

  1. {
  2. "createdAt": "2014-10-16T02:31:42.399Z",
  3. "updatedAt": "2014-10-16T02:43:47.360Z",
  4. "id": "543f2e0e474f229d61185565",
  5. "name": "manager1",
  6. "description": "manager desc"
  7. }

删除角色

您可以通过发送一个 DELETE 请求来删除某个已创建的角色对象,示例如下:

curl

  1. curl -X DELETE \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. https://d.apicloud.com/mcm/api/role/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("role");
  3. Model.delete({"_id":"543f2e0e474f229d61185565"},function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/role/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"_method":"DELETE"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

如果执行成功,则返回200状态码。

ACL

待补充

批量操作

为了减少网络交互的次数太多带来的时间浪费, 您可以在一个请求中对多个对象进行 create/update/delete 操作.

在一个批次中每一个操作都有相应的方法、路径和主体, 这些参数可以代替您通常会使用的 HTTP 方法. 这些操作会以发送过去的顺序来执行, 比如我们要创建一系列的 GameScore 的对象:

curl

  1. curl -X POST \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -H "Content-Type: application/json" \
  5. -d '{
  6. "requests": [
  7. {
  8. "method": "POST",
  9. "path": "/mcm/api/company",
  10. "body": {
  11. "name": "apicloud",
  12. "address": "北京市..."
  13. }
  14. },
  15. {
  16. "method": "POST",
  17. "path": "/mcm/api/company",
  18. "body": {
  19. "name": "百度",
  20. "address": "北京市西二旗"
  21. }
  22. }
  23. ]
  24. }' \
  25. https://d.apicloud.com/mcm/api/batch

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. client.batch([
  3. {
  4. "method": "POST",
  5. "path": "/mcm/api/company",
  6. "body": {
  7. "name": "apicloud",
  8. "address": "北京市..."
  9. }
  10. },
  11. {
  12. "method": "POST",
  13. "path": "/mcm/api/company",
  14. "body": {
  15. "name": "百度",
  16. "address": "北京市西二旗"
  17. }
  18. }
  19. ], function(ret,err){
  20. if(err){
  21. //处理错误 err
  22. }else{
  23. //处理数据 ret
  24. }
  25. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/batch",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {
  10. "requests": [{
  11. "method": "POST",
  12. "path": "/mcm/api/company",
  13. "body": {
  14. "name": "apicloud",
  15. "address": "北京市..."
  16. }
  17. },
  18. {
  19. "method": "POST",
  20. "path": "/mcm/api/company",
  21. "body": {
  22. "name": "百度",
  23. "address": "北京市西二旗"
  24. }
  25. }]
  26. }
  27. }).done(function (data, status, header) {
  28. //success body
  29. }).fail(function (header, status, errorThrown) {
  30. //fail body
  31. })

批量操作的响应会是一个列表, 列表的元素数量和给定的操作数量是一致的. 每一个在列表中的元素都是一个对象.”success” 的值是通常是进行其他 REST 操作会返回的值:

  1. {
  2. "createdAt": "2012-06-15T16:59:11.276Z",
  3. "id": "51c3ba67e4b0f0e851c16221"
  4. }

“error” 的值会是一个对象有返回码和 “error” 字符串:

  1. {
  2. "error": {
  3. "code": 101,
  4. "error": ""
  5. }
  6. }

文件(file)

表结构

  • name文件名称(带后缀)
  • url:文件访问地址
  • type:文件类型
  • size:文件大小(字节)

相关参数

请求地址: http://d.apicloud.com/mcm/api/file

参数:

  1. "X-APICloud-AppId: {{your_app_id}}"
  2. "X-APICloud-AppKey: {{加密后的key}}"
  3. "filename": file.name,
  4. "type": file.type

上传示例

curl示例

  1. curl -X POST \
  2. -H "X-APICloud-AppId:{{your_app_id}}" \
  3. -H "X-APICloud-AppKey:{{加密后的key}}" \
  4. -H "application/x-www-form-urlencoded;" \
  5. -F "file=@{{file.path}};filename={{file.filename}};type={{file.type}}" \
  6. https://d.apicloud.com/mcm/api/file

js-sdk

  1. var File = client.Factory("file");
  2. api.getPicture({},function(ret,err){
  3. File.save({file:{isFile:true,path:ret.data,values:{filename:"ava.png"}}},function(data,err){
  4. alert("file:\t"+JSON.stringify(data));
  5. alert("file:\t"+JSON.stringify(err));
  6. })
  7. })

web示例

  1. <div id="picker"></div>
  1. var uploadurl = WebUploader.create({
  2. pick: "#picker",
  3. swf: '/libs/webuploader/Uploader.swf',
  4. server: 'http://d.apicloud.com/mcm/api/file',
  5. resize: false,
  6. auto: true
  7. });
  8. uploadurl.on("fileQueued", function (file) {
  9. uploadurl.option('formData', {
  10. filename: file.name,
  11. type: file.type
  12. });
  13. });
  14. //文件上传成功
  15. uploadurl.on('uploadSuccess', function (file, res) {
  16. if (res && res.id) {
  17. alert("成功")
  18. } else if (res &&res.status == 0) {
  19. alert("失败")
  20. } else {
  21. alert("失败")
  22. }
  23. });
  24. //文件上传失败
  25. uploadurl.on('uploadError', function (file, reason) {
  26. alert("失败")
  27. });
  28. //上传完成,不管成功失败
  29. uploadurl.on('uploadComplete', function (file) {
  30. uploadurl.removeFile(file);
  31. });
  32. uploadurl.on('uploadBeforeSend', function (block, data, headers) {
  33. headers["X-APICloud-AppKey"] = "{{加密后的key}}";
  34. headers["X-APICloud-AppId"] = "{{your_app_id}}";
  35. });
  36. //上传中
  37. uploadurl.on('uploadProgress',function(file,percentage){
  38. });

提示:示例中使用的是WebUploader http://fex.baidu.com/webuploader/

成功后返回:

  1. {
  2. "createdAt": "2014-12-02T10:19:47.308Z",
  3. "updatedAt": "2014-12-02T10:19:47.308Z",
  4. "id": "547d924304fca43974ca87a5",
  5. "url": "http://file.apicloud.com/mcm/A6961095046004/a6c6cef31b5aa903db7e2788ce74f29d.png",
  6. "name": "2.png",
  7. "type": "image/png",
  8. "size": 3798,
  9. "filename": "2.png",
  10. "lastModifiedDate": "Tue Apr 08 2014 12:23:20 GMT+0800 (中国标准时间)"
  11. }

扩展

如在其他表中有File类型的字段,添加记录可以如下添加:

jquery.ajax

  1. var file={
  2. id: "547d92ed04fca43974ca87a6"
  3. name: "2.png"
  4. url: "http://file.apicloud.com/mcm/A6961095046004/a6c6cef31b5aa903db7e2788ce74f29d.png"
  5. }
  6. var options={
  7. url:"http://d.apicloud.com/mcm/api/{{className}}",
  8. method:"POST",
  9. data:{
  10. "{{fileColumnName}}":file
  11. ...
  12. },
  13. headers:{
  14. "X-APICloud-AppKey" = "{{加密后的key}}";
  15. "X-APICloud-AppId" = "{{your_app_id}}";
  16. }
  17. }
  18. $.ajax(options).done(function(data){
  19. //新增后的处理
  20. })

结果

  1. {
  2. "id": "547d92ed04fca43974ca87a7",
  3. "createdAt": "2014-12-02T10:22:37.379Z",
  4. "updatedAt": "2014-12-02T10:22:37.379Z",
  5. "file": {
  6. "url": "http://file.apicloud.com/mcm/A6961095046004/a6c6cef31b5aa903db7e2788ce74f29d.png",
  7. "name": "2.png",
  8. "id": "547d92ed04fca43974ca87a6"
  9. }
  10. }

更新操作符

操作符

名称说明
$inc增加字段的值指定的数量
$mul增加字段的值乘以指定的数量
$set在文档中设置一个字段的值。
$min仅更新字段如果指定的值小于现有的字段值。
$max仅更新字段如果指定的值大于现有的字段值。
$push增加一个项到数组的尾部
$pushAll增加多个项到数组的尾
$pull删除一个项从数组当中
$pullAll删除多个项从数组中

$inc


$inc操作符增加一个字段指定值和具有以下形式:

  1. { $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }

行为

$inc是一个原子操作在一个文档中.

Example

考虑有如下一个集合:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "sku": "abc123",
  4. "quantity": 10
  5. }

使用$inc进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$inc": { quantity: -2},"sku":"APICloud"}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$inc": { quantity: -2},"sku":"APICloud"}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$inc": { quantity: -2},"sku":"APICloud","_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id" : 543f2e0e474f229d61185565,
  3. "sku" : "APICloud",
  4. "quantity" : 8,
  5. }

$mul


$mul操作符增加一个字段乘以指定值和具有以下形式:

  1. { $mul: { <field1>: <amount1>, <field2>: <amount2>, ... } }

行为

$mul是一个原子操作在一个文档中.

Example

考虑有如下一个集合:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "item": "ABC",
  4. "price": 10.99
  5. }

使用$mul进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$mul": { "price": 1.25 }}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$mul": { "price": 1.25 }}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$mul": { "price": 1.25 },"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id": 1,
  3. "item": "ABC",
  4. "price": 13.7375
  5. }

$set


$set操作符替换一个字段的值用指定的值,具有以下形式:

  1. { $set: { <field1>: <amount1>, <field2>: <amount2>, ... } }

Example

考虑有如下一个集合:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "sku": "abc123",
  4. "quantity": 250,
  5. "instock": true,
  6. "reorder": false,
  7. "details": { model: "14Q2", make: "xyz" },
  8. "tags": [ "apparel", "clothing" ],
  9. "ratings": [ { by: "ijk", rating: 4 } ]
  10. }

使用$set进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$set": {quantity: 500,details: { model: "14Q3", make: "xyz" },tags: [ "coats", "outerwear", "clothing" ]}}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{
  4. "$set": {
  5. quantity: 500,
  6. details: {
  7. model: "14Q3",
  8. make: "xyz"
  9. },
  10. tags: [
  11. "coats",
  12. "outerwear",
  13. "clothing"
  14. ]
  15. }
  16. }, function(ret,err){
  17. if(err){
  18. //处理错误 err
  19. }else{
  20. //处理数据 ret
  21. }
  22. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {
  10. "$set": {
  11. quantity: 500,
  12. details: {
  13. model: "14Q3",
  14. make: "xyz"
  15. },
  16. tags: [
  17. "coats",
  18. "outerwear",
  19. "clothing"
  20. ]
  21. },
  22. "_method":"PUT"
  23. }
  24. }).done(function (data, status, header) {
  25. //success body
  26. }).fail(function (header, status, errorThrown) {
  27. //fail body
  28. })

更新后的文档会像:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "sku": "abc123",
  4. "quantity": 500,
  5. "instock": true,
  6. "reorder": false,
  7. "details": { model: "14Q3", make: "xyz" },
  8. "tags": [ "coats", "outerwear", "clothing" ],
  9. "ratings": [ { by: "ijk", rating: 4 } ]
  10. }

$min


$min操作符更新字段的值为一个指定的值,如果值小于指定字段的当前值,具有以下形式:

  1. { $min: { <field1>: <amount1>, <field2>: <amount2>, ... } }

Example

考虑有如下一个集合:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "highScore": 800,
  4. "lowScore": 200
  5. }

使用$min进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$min": { "lowScore": 150 }}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$min": { "lowScore": 150 }}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$min": { "lowScore": 150 },"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "highScore": 800,
  4. "lowScore": 150
  5. }

$max


$max操作符更新字段的值指定的值如果指定的值大于字段的当前值,具有以下形式:

  1. { $max: { <field1>: <amount1>, <field2>: <amount2>, ... } }

Example

考虑有如下一个集合:

  1. {
  2. "_id": "543f2e0e474f229d61185565",
  3. "highScore": 800,
  4. "lowScore": 200
  5. }

使用$set进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$max": { "highScore": 950 }}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$max": { "highScore": 950 }}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$max": { "highScore": 950 },"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id": 1,
  3. "highScore": 950,
  4. "lowScore": 200
  5. }

$push


$push操作符将指定值添加到数组中,具有以下形式:

  1. { $push: { <field1>: <value1>, ... } }

行为

如果字段不是数组类型,操作将会失败。

Example

考虑有如下一个集合:

  1. {
  2. "_id" : "543f2e0e474f229d61185565",
  3. "quizzes" : [
  4. { "wk": 1, "score" : 10 },
  5. { "wk": 2, "score" : 8 },
  6. { "wk": 3, "score" : 5 },
  7. { "wk": 4, "score" : 6 }
  8. ]
  9. }

使用$push进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$push": { quizzes: { "wk": 5, "score": 8 }}}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$push": { quizzes: { "wk": 5, "score": 8 }}}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$push": { quizzes: { "wk": 5, "score": 8 }},"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id" : "543f2e0e474f229d61185565",
  3. "quizzes" : [
  4. { "wk": 1, "score" : 10 },
  5. { "wk": 2, "score" : 8 },
  6. { "wk": 3, "score" : 5 },
  7. { "wk": 4, "score" : 6 },
  8. { "wk": 5, "score" : 8 }
  9. ]
  10. }

$pushAll


$pushAll操作符将数组添加到数组中,具有以下形式:

  1. { $pushAll: { <field>: [ <value1>, <value2>, ... ] } }

行为

如果字段不是数组类型,操作将会失败。

Example

考虑有如下一个集合:

  1. {
  2. "_id" : "543f2e0e474f229d61185565",
  3. "quizzes" : [
  4. { "wk": 1, "score" : 10 },
  5. { "wk": 2, "score" : 8 },
  6. { "wk": 3, "score" : 5 },
  7. { "wk": 4, "score" : 6 }
  8. ]
  9. }

使用$pushAll进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$pushAll": { quizzes: [{ "wk": 5, "score": 8 },{ "wk": 6, "score": 14 }]}}' \
  5. https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$pushAll": { quizzes: [{ "wk": 5, "score": 8 },{ "wk": 6, "score": 14 }]}}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$pushAll": { quizzes: [{ "wk": 5, "score": 8 },{ "wk": 6, "score": 14 }]},"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id" : "543f2e0e474f229d61185565",
  3. "quizzes" : [
  4. { "wk": 1, "score" : 10 },
  5. { "wk": 2, "score" : 8 },
  6. { "wk": 3, "score" : 5 },
  7. { "wk": 4, "score" : 6 },
  8. { "wk": 5, "score" : 8 },
  9. { "wk": 6, "score" : 14 }
  10. ]
  11. }

$pull


$pull操作符将指定项从数组中删除,具有以下形式:

  1. { $pull: { <field1>: value, <field2>: value, ... } }

行为

如果字段不是数组类型,操作将会失败。

Example

考虑有如下一个集合:

  1. {
  2. _id: "543f2e0e474f229d61185565",
  3. fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ],
  4. vegetables: [ "carrots", "celery", "squash", "carrots" ]
  5. }

使用$pull进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$pull": { "fruits":"apples", "vegetables": "carrots" }}' \
  5. https://d.apicloud.com/mcm/api/modelName/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$pull": { "fruits":"apples", "vegetables": "carrots" }}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$pull": { "fruits":"apples", "vegetables": "carrots" },"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id" : "543f2e0e474f229d61185565",
  3. "fruits" : [ "pears","oranges", "grapes", "bananas" ],
  4. "vegetables" : [ "celery", "squash" ]
  5. }

$pullAll


$pullAll 操作符将指定项从数组中删除,具有以下形式:

  1. { $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }

行为

如果字段不是数组类型,操作将会失败。

Example

考虑有如下一个集合:

  1. {
  2. _id: "543f2e0e474f229d61185565",
  3. "fruits": [ "apples", "pears", "oranges", "grapes", "bananas" ],
  4. "vegetables": [ "carrots", "celery", "squash", "carrots" ]
  5. }

使用$pullAll进行更新

curl

  1. curl -X PUT \
  2. -H "X-APICloud-AppId: {{your_app_id}}" \
  3. -H "X-APICloud-AppKey: {{加密后的key}}" \
  4. -d '{"$pullAll": { "fruits": [ "apples", "oranges" ]}}' \
  5. https://d.apicloud.com/mcm/api/modelName/543f2e0e474f229d61185565

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.save({"_id":"543f2e0e474f229d61185565"},{"$pullAll": { "fruits": [ "apples", "oranges" ]}}, function(ret,err){
  4. if(err){
  5. //处理错误 err
  6. }else{
  7. //处理数据 ret
  8. }
  9. })

jquery.ajax

  1. $.ajax({
  2. "url": "https://d.apicloud.com/mcm/api/Company/543f2e0e474f229d61185565",
  3. "type": "POST",
  4. "cache": false,
  5. "headers": {
  6. "X-APICloud-AppId": "{{your_app_id}}",
  7. "X-APICloud-AppKey": "{{加密后的key}}"
  8. },
  9. "data": {"$pullAll": { "fruits": [ "apples", "oranges" ]},"_method":"PUT"}
  10. }).done(function (data, status, header) {
  11. //success body
  12. }).fail(function (header, status, errorThrown) {
  13. //fail body
  14. })

更新后的文档会像:

  1. {
  2. "_id" : "543f2e0e474f229d61185565",
  3. "fruits" : [ "pears", "grapes", "bananas" ],
  4. "vegetables": [ "carrots", "celery", "squash", "carrots" ]
  5. }

查询

类型(filterType)

  • fields
  • limit
  • order
  • skip
  • where
  • include
  • includefilter

请求语法

REST 语法

  1. filterType=spec&filterType=spec....

example

  1. GET /mcm/api/user?filter[where][id]=1234

Stringified 语法

  1. { filterType: spec, filterType: spec, ... }

example

  1. GET /mcm/api/user?filter={"where":{"id":1234}}

字段过滤(Fields filter)

设置字段是否显示在结果列表内。

REST 语法

  1. filter[fields][propertyName]=<true|false>&filter[fields][propertyName]=<true|false>...

Stringified 语法

  1. filter={ "fields": {"propertyName": <true|false>, "propertyName": <true|false>, ... } }

Examples

只显示id,make,model字段

REST 语法

  1. GET /mcm/api/car?filter[fields][id]=true&filter[fields][make]=true&filter[fields][model]=true

Stringified 语法

  1. GET /mcm/api/car?filter={ "fields": {"id": true, "make": true, "model": true} }

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:
  5. fields:{"id": true, "make": true, "model": true}//或者["id","make","model"]
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. fields:{"id": true, "make": true, "model": true}//或者["id","make","model"]
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

Returns:

  1. [
  2. {
  3. "id": "1",
  4. "make": "Nissan",
  5. "model": "Titan"
  6. },
  7. {
  8. "id": "2",
  9. "make": "Nissan",
  10. "model": "Avalon"
  11. },
  12. ...
  13. ]

排除vin字段

REST 语法

  1. GET /mcm/api/car?filter[fields][vin]=false

Stringified 语法

  1. GET /mcm/api/car?filter={ "fields": {"vin": false} }

Examples

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. fields:{"vin": false}
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. fields:{"vin": false}
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

条数过滤(Limit filter)

限制返回的记录数指定的数量(或更少)

REST 语法

  1. filter[limit]=n

Stringified 语法

  1. filter={"limit": n}

Examples

返回前5条结果

REST 语法

  1. GET /mcm/api/cars?filter[limit]=5

Stringified 语法

  1. GET /mcm/api/cars?filter={"limit": 5}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. limit:5
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. limit:5
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

排序(Order filter)

REST 语法

一列

  1. filter[order]=propertyName<ASC|DESC>

两列

  1. filter[order][0]=propertyName <ASC|DESC>&filter[order][1]=propertyName <ASC|DESC>...

Stringified 语法

一列

  1. filter={ "order": "propertyName <ASC|DESC>" }

两列

  1. filter={ "order": ["propertyName <ASC|DESC>", "propertyName <ASC|DESC>",...] }

Examples

按audibleRange 进行倒叙排序,并限制返回3条数据

REST 语法

  1. GET /mcm/api/weapons?filter[order]=audibleRange DESC&filter[limit]=3

Stringified 语法

  1. GET /mcm/api/weapons?filter={"order": "audibleRange DESC", "limit": 3 }

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "order": "audibleRange DESC",
  6. "limit": 3
  7. }
  8. }, function(ret,err){
  9. if(err){
  10. //处理错误 err
  11. }else{
  12. //处理数据 ret
  13. }
  14. })

jquery.ajax

  1. var filter = {
  2. "order": "audibleRange DESC",
  3. "limit": 3
  4. }
  5. $.ajax({
  6. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  7. "type": "GET",
  8. "cache": false,
  9. "headers": {
  10. "X-APICloud-AppId": "{{your_app_id}}",
  11. "X-APICloud-AppKey": "{{加密后的key}}"
  12. }
  13. }).done(function (data, status, header) {
  14. //success body
  15. }).fail(function (header, status, errorThrown) {
  16. //fail body
  17. })

跳过(Skip filter)

跳过一些数据后返回

REST

  1. filter[skip]=n

Stringified 语法

  1. filter={ "skip": n }

Examples

REST 语法

  1. GET /mcm/api/cars?filter[limit]=10&filter[skip]=20

Stringified 语法

  1. GET /mcm/api/cars?filter={"limit":10,"skip":20}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "limit":10,
  6. "skip":20
  7. }
  8. }, function(ret,err){
  9. if(err){
  10. //处理错误 err
  11. }else{
  12. //处理数据 ret
  13. }
  14. })

jquery.ajax

  1. var filter = {
  2. "limit":10,
  3. "skip":20
  4. }
  5. $.ajax({
  6. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  7. "type": "GET",
  8. "cache": false,
  9. "headers": {
  10. "X-APICloud-AppId": "{{your_app_id}}",
  11. "X-APICloud-AppKey": "{{加密后的key}}"
  12. }
  13. }).done(function (data, status, header) {
  14. //success body
  15. }).fail(function (header, status, errorThrown) {
  16. //fail body
  17. })

条件过滤(Where filter)

在过滤器指定一组逻辑条件匹配,类似于一个where子句的SQL查询

REST 语法

  1. filter[where][property]=value
  2. filter[where][property][op]=value

Examples

  1. GET /mcm/api/cars?filter[where][carClass]=fullsize

Stringified 语法

  1. filter={"where": {"property": value}}
  2. filter={"where": {"property": {"op": value}}}

Examples

  1. GET /mcm/api/cars?filter={"where":{"carClass":"fullsize"}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where":{"carClass":"fullsize"}
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. "where":{"carClass":"fullsize"}
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

操作符

操作符说明
and逻辑与
or逻辑或
gt,gte大于(>),大于或等于(> =)。只有效数值和日期值
lt,lte小于(<),小于或等于(< =)。只有效数值和日期值
between在…之间
inq,nin在/不在一个数组之内
ne不等于(!=)
like,nlikelike/not like 操作符返回符合正则表达式的数据

Examples

equal

REST 语法

  1. GET /mcm/api/weapons?filter[where][name]=M1911

Stringified 语法

  1. GET /mcm/api/weapons?filter={"where":{"name":"M1911"}}

gt and lt

REST 语法

  1. GET /mcm/api/weapons?filter[where][effectiveRange][gt]=900
  2. GET /mcm/api/weapons?filter[where][audibleRange][lt]=10

Stringified 语法

  1. GET /mcm/api/weapons?filter={"where":{"effectiveRange":{"gt":900}}}
  2. GET /mcm/api/weapons?filter={"where":{"audibleRange":{"lt":10}}}

Examples

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where":{
  6. "effectiveRange":{"gt":900},
  7. "audibleRange":{"lt":10}
  8. }
  9. }
  10. }, function(ret,err){
  11. if(err){
  12. //处理错误 err
  13. }else{
  14. //处理数据 ret
  15. }
  16. })

jquery.ajax

  1. var filter = {
  2. "where":{
  3. "effectiveRange":{"gt":900},
  4. "audibleRange":{"lt":10}
  5. }
  6. }
  7. $.ajax({
  8. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  9. "type": "GET",
  10. "cache": false,
  11. "headers": {
  12. "X-APICloud-AppId": "{{your_app_id}}",
  13. "X-APICloud-AppKey": "{{加密后的key}}"
  14. }
  15. }).done(function (data, status, header) {
  16. //success body
  17. }).fail(function (header, status, errorThrown) {
  18. //fail body
  19. })

and / or

语法

  1. [where][<and|or>][0]condition1&[where][<and|or>]condition2...

REST 语法

  1. GET /mcm/api/cars?filter[where][and][0][title]=My%20Post&filter[where][and][1][content]=Hello

Stringified 语法

  1. GET /mcm/api/cars?filter={"where": {"and": [{"title": "My Post"}, {"content": "Hello"}]}}

Examples

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where": {
  6. "and": [
  7. {"title": "My Post"},
  8. {"content": "Hello"}
  9. ]
  10. }
  11. }
  12. }, function(ret,err){
  13. if(err){
  14. //处理错误 err
  15. }else{
  16. //处理数据 ret
  17. }
  18. })

jquery.ajax

  1. var filter = {
  2. "where": {
  3. "and": [
  4. {"title": "My Post"},
  5. {"content": "Hello"}
  6. ]
  7. }
  8. }
  9. $.ajax({
  10. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  11. "type": "GET",
  12. "cache": false,
  13. "headers": {
  14. "X-APICloud-AppId": "{{your_app_id}}",
  15. "X-APICloud-AppKey": "{{加密后的key}}"
  16. }
  17. }).done(function (data, status, header) {
  18. //success body
  19. }).fail(function (header, status, errorThrown) {
  20. //fail body
  21. })

between

REST 语法

  1. GET /mcm/api/cars?filter[where][price][between][0]=0&filter[where][price][between][1]=7

Stringified 语法

  1. GET /mcm/api/cars?filter={"where":{"price":{"between":[0,7]}}}

Examples

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where": {
  6. "price":{"between":[0,7]}
  7. }
  8. }
  9. }, function(ret,err){
  10. if(err){
  11. //处理错误 err
  12. }else{
  13. //处理数据 ret
  14. }
  15. })

jquery.ajax

  1. var filter = {
  2. "where": {
  3. "price":{"between":[0,7]}
  4. }
  5. }
  6. $.ajax({
  7. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  8. "type": "GET",
  9. "cache": false,
  10. "headers": {
  11. "X-APICloud-AppId": "{{your_app_id}}",
  12. "X-APICloud-AppKey": "{{加密后的key}}"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

like and nlike

REST 语法

  1. GET /mcm/api/Post?filter[where][title][like]=M.+st
  2. GET /mcm/api/Post?filter[where][title][nlike]=M.+XY
  3. GET /mcm/api/User?filter[where][name][like]=%St%
  4. GET /mcm/api/User?filter[where][name][nlike]=M%XY

Stringified 语法

  1. GET /mcm/api/Post?filter={"where":{"title": {"like": "M.+st"}}}
  2. GET /mcm/api/Post?filter={"where":{"title": {"nlike": "M.+XY"}}}
  3. GET /mcm/api/User?filter={"where": {"name": {"like": "%St%"}}}
  4. GET /mcm/api/User?filter={"where": {"name": {"nlike": "M%XY"}}}

Examples

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where":{
  6. "title":{
  7. "like":"M.+st"
  8. },
  9. "name::{
  10. "nlike":"M%XY"
  11. }
  12. }
  13. }
  14. }, function(ret,err){
  15. if(err){
  16. //处理错误 err
  17. }else{
  18. //处理数据 ret
  19. }
  20. })

jquery.ajax

  1. var filter = {
  2. "where":{
  3. "title":{
  4. "like":"M.+st"
  5. },
  6. "name::{
  7. "nlike":"M%XY"
  8. }
  9. }
  10. }
  11. $.ajax({
  12. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  13. "type": "GET",
  14. "cache": false,
  15. "headers": {
  16. "X-APICloud-AppId": "{{your_app_id}}",
  17. "X-APICloud-AppKey": "{{加密后的key}}"
  18. }
  19. }).done(function (data, status, header) {
  20. //success body
  21. }).fail(function (header, status, errorThrown) {
  22. //fail body
  23. })

inq

REST 语法

  1. GET /mcm/api/medias?filter[where][keywords][inq]=foo&filter[where][keywords][inq]=bar

Stringified 语法

  1. GET /mcm/api/medias?filter={"where": {"keywords": {"inq": ["foo", "bar"]}}}

Examples

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where":{
  6. "keywords": {"inq": ["foo", "bar"]}
  7. }
  8. }
  9. }, function(ret,err){
  10. if(err){
  11. //处理错误 err
  12. }else{
  13. //处理数据 ret
  14. }
  15. })

jquery.ajax

  1. var filter = {
  2. "where":{
  3. "keywords": {"inq": ["foo", "bar"]}
  4. }
  5. }
  6. $.ajax({
  7. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  8. "type": "GET",
  9. "cache": false,
  10. "headers": {
  11. "X-APICloud-AppId": "{{your_app_id}}",
  12. "X-APICloud-AppKey": "{{加密后的key}}"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

关系过滤(Include filter)

查询relation,pointer相关的数据

提示:pointer类型的列查询时需要在列名后增加Pointer,例如列名author,查询时就是authorPointer

REST 语法

  1. GET /mcm/api/cars?filter[include][relatedModel]=propertyName

Stringified 语法

  1. GET /mcm/api/cars?filter={"include": "relatedModel"}
  2. GET /mcm/api/cars?filter={"include": ["relatedModel1", "relatedModel2", ...]}
  3. GET /mcm/api/cars?filter={"include": {"relatedModel1": [{"relatedModel2": "propertyName"} , "relatedModel"]}}

Examples

REST 语法

  1. GET /mcm/api/members?filter[include]=posts

Stringified 语法

  1. GET /mcm/api/members?filter={"include":"posts"}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":"posts"
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. "include":"posts"
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1
  11. },
  12. {
  13. "title": "Post B",
  14. "id": 2,
  15. "memberId": 1
  16. },
  17. {
  18. "title": "Post C",
  19. "id": 3,
  20. "memberId": 1
  21. }
  22. ]
  23. },
  24. {
  25. "name": "Member B",
  26. "age": 22,
  27. "id": 2,
  28. "posts": [
  29. {
  30. "title": "Post D",
  31. "id": 4,
  32. "memberId": 2
  33. }
  34. ]
  35. },
  36. ... ]

REST 语法

  1. GET /mcm/api/members?filter[include][posts]=authorPointer

Stringified 语法

  1. GET /mcm/api/members?filter={"include":{"posts":"authorPointer"}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":{"posts":"authorPointer"}
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. "include":{"posts":"authorPointer"}
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1,
  11. "authorPointer": {
  12. "name": "Member A",
  13. "age": 21,
  14. "id": 1
  15. }
  16. },
  17. {
  18. "title": "Post B",
  19. "id": 2,
  20. "memberId": 1,
  21. "authorPointer": {
  22. "name": "Member A",
  23. "age": 21,
  24. "id": 1
  25. }
  26. },
  27. {
  28. "title": "Post C",
  29. "id": 3,
  30. "memberId": 1,
  31. "authorPointer": {
  32. "name": "Member A",
  33. "age": 21,
  34. "id": 1
  35. }
  36. }
  37. ]
  38. },
  39. {
  40. "name": "Member B",
  41. "age": 22,
  42. "id": 2,
  43. "posts": [
  44. {
  45. "title": "Post D",
  46. "id": 4,
  47. "memberId": 2,
  48. "authorPointer": {
  49. "name": "Member B",
  50. "age": 22,
  51. "id": 2
  52. }
  53. }
  54. ]
  55. }, ... ]

REST 语法

  1. GET /api/members?filter[include][posts]=authorPointer&filter[where][age]=21

Stringified 语法

  1. GET /api/members?filter={"include":{"posts":"authorPointer"},"where":{"age":21}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":{"posts":"authorPointer"},
  6. "where":{"age":21}
  7. }
  8. }, function(ret,err){
  9. if(err){
  10. //处理错误 err
  11. }else{
  12. //处理数据 ret
  13. }
  14. })

jquery.ajax

  1. var filter = {
  2. "include":{"posts":"authorPointer"},
  3. "where":{"age":21}
  4. }
  5. $.ajax({
  6. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  7. "type": "GET",
  8. "cache": false,
  9. "headers": {
  10. "X-APICloud-AppId": "{{your_app_id}}",
  11. "X-APICloud-AppKey": "{{加密后的key}}"
  12. }
  13. }).done(function (data, status, header) {
  14. //success body
  15. }).fail(function (header, status, errorThrown) {
  16. //fail body
  17. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1,
  11. "authorPointer": {
  12. "name": "Member A",
  13. "age": 21,
  14. "id": 1
  15. }
  16. },
  17. {
  18. "title": "Post B",
  19. "id": 2,
  20. "memberId": 1,
  21. "authorPointer": {
  22. "name": "Member A",
  23. "age": 21,
  24. "id": 1
  25. }
  26. },
  27. {
  28. "title": "Post C",
  29. "id": 3,
  30. "memberId": 1,
  31. "authorPointer": {
  32. "name": "Member A",
  33. "age": 21,
  34. "id": 1
  35. }
  36. }
  37. ]
  38. }
  39. ]

REST 语法

  1. GET /mcm/api/members?filter[include][posts]=authorPointer&filter[limit]=2

Stringified 语法

  1. GET /mcm/api/members?filter={"include":{"posts":"authorPointer"},"limit":2}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":{"posts":"authorPointer"},
  6. "limit":2
  7. }
  8. }, function(ret,err){
  9. if(err){
  10. //处理错误 err
  11. }else{
  12. //处理数据 ret
  13. }
  14. })

jquery.ajax

  1. var filter = {
  2. "include":{"posts":"authorPointer"},
  3. "limit":2
  4. }
  5. $.ajax({
  6. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  7. "type": "GET",
  8. "cache": false,
  9. "headers": {
  10. "X-APICloud-AppId": "{{your_app_id}}",
  11. "X-APICloud-AppKey": "{{加密后的key}}"
  12. }
  13. }).done(function (data, status, header) {
  14. //success body
  15. }).fail(function (header, status, errorThrown) {
  16. //fail body
  17. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1,
  11. "authorPointer": {
  12. "name": "Member A",
  13. "age": 21,
  14. "id": 1
  15. }
  16. },
  17. {
  18. "title": "Post B",
  19. "id": 2,
  20. "memberId": 1,
  21. "authorPointer": {
  22. "name": "Member A",
  23. "age": 21,
  24. "id": 1
  25. }
  26. },
  27. {
  28. "title": "Post C",
  29. "id": 3,
  30. "memberId": 1,
  31. "authorPointer": {
  32. "name": "Member A",
  33. "age": 21,
  34. "id": 1
  35. }
  36. }
  37. ]
  38. },
  39. {
  40. "name": "Member B",
  41. "age": 22,
  42. "id": 2,
  43. "posts": [
  44. {
  45. "title": "Post D",
  46. "id": 4,
  47. "memberId": 2,
  48. "authorPointer": {
  49. "name": "Member B",
  50. "age": 22,
  51. "id": 2
  52. }
  53. }
  54. ]
  55. }
  56. ]

REST 语法

  1. GET /mcm/api/members?filter[include]=posts&filter[include]=passports

Stringified 语法

  1. GET /mcm/api/members?filter={"include":["posts","passports"]}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":["posts","passports"]
  6. }
  7. }, function(ret,err){
  8. if(err){
  9. //处理错误 err
  10. }else{
  11. //处理数据 ret
  12. }
  13. })

jquery.ajax

  1. var filter = {
  2. "include":["posts","passports"]
  3. }
  4. $.ajax({
  5. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  6. "type": "GET",
  7. "cache": false,
  8. "headers": {
  9. "X-APICloud-AppId": "{{your_app_id}}",
  10. "X-APICloud-AppKey": "{{加密后的key}}"
  11. }
  12. }).done(function (data, status, header) {
  13. //success body
  14. }).fail(function (header, status, errorThrown) {
  15. //fail body
  16. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1
  11. },
  12. {
  13. "title": "Post B",
  14. "id": 2,
  15. "memberId": 1
  16. },
  17. {
  18. "title": "Post C",
  19. "id": 3,
  20. "memberId": 1
  21. }
  22. ],
  23. "passports": [
  24. {
  25. "number": "1",
  26. "id": 1,
  27. "ownerId": 1
  28. }
  29. ]
  30. },
  31. {
  32. "name": "Member B",
  33. "age": 22,
  34. "id": 2,
  35. "posts": [
  36. {
  37. "title": "Post D",
  38. "id": 4,
  39. "memberId": 2
  40. }
  41. ],
  42. "passports": [
  43. {
  44. "number": "2",
  45. "id": 2,
  46. "ownerId": 2
  47. }
  48. ]
  49. }, ... ]

关系表过滤(Include filter)

对relation,pointer相关的数据进行过滤,必须配合include使用

REST 语法

  1. GET /mcm/api/cars?filter[include][relatedModel]=propertyName&filter[includefilter][relatedModelName][limit]=2

Stringified 语法

  1. GET /mcm/api/cars?filter={"include": "relatedModel","includefilter":{"relatedModelName":{"limit":2}}}
  2. GET /mcm/api/cars?filter={"include": ["relatedModel1", "relatedModel2", ...],"includefilter":{"relatedModel1Name":{"limit":2},"relatedModel2Name":{"where":{"propertyName":"username"}}}}
  3. GET /mcm/api/cars?filter={"include": {"relatedModel1": [{"relatedModel2": "propertyName"} , "relatedModel"]},"includefilter":{"relatedModel1Name":{"limit":2},"relatedModel2Name":{"where":{"propertyName":"username"}},"relatedModelName":{"limit":2}}}

提示 relatedModel1Name 为relation或者pointer的class的名称,如果是relation的表,则id为必须要返回的字段,如果是pointer的表,则id与pointer的字段为必须返回字段

Examples

REST 语法

  1. GET /mcm/api/members?filter[include]=posts&filter[includefilter][Posts][limit]=2

Stringified 语法

  1. GET /mcm/api/members?filter={"include":"posts","includefilter":{"Posts":{"limit":2}}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":"posts",
  6. "includefilter":{"Posts":{"limit":2}}
  7. }
  8. }, function(ret,err){
  9. if(err){
  10. //处理错误 err
  11. }else{
  12. //处理数据 ret
  13. }
  14. })

jquery.ajax

  1. var filter = {
  2. "include":"posts",
  3. "includefilter":{"Posts":{"limit":2}}
  4. }
  5. $.ajax({
  6. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  7. "type": "GET",
  8. "cache": false,
  9. "headers": {
  10. "X-APICloud-AppId": "{{your_app_id}}",
  11. "X-APICloud-AppKey": "{{加密后的key}}"
  12. }
  13. }).done(function (data, status, header) {
  14. //success body
  15. }).fail(function (header, status, errorThrown) {
  16. //fail body
  17. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1
  11. },
  12. {
  13. "title": "Post B",
  14. "id": 2,
  15. "memberId": 1
  16. }
  17. ]
  18. },
  19. {
  20. "name": "Member B",
  21. "age": 22,
  22. "id": 2,
  23. "posts": [
  24. {
  25. "title": "Post D",
  26. "id": 4,
  27. "memberId": 2
  28. }
  29. ]
  30. },
  31. ... ]

REST 语法

  1. GET /mcm/api/members?filter[include][posts]=authorPointer&filter["includefilter"][author][fields]=id&filter["includefilter"][author][fields]=name

Stringified 语法

  1. GET /mcm/api/members?filter={"include":{"posts":"authorPointer"},"includefilter":{"author":{"fields":["id","name"]}}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":{"posts":"authorPointer"},
  6. "includefilter":{"author":{"fields":["id","name"]}}
  7. }
  8. }, function(ret,err){
  9. if(err){
  10. //处理错误 err
  11. }else{
  12. //处理数据 ret
  13. }
  14. })

jquery.ajax

  1. var filter = {
  2. "include":{"posts":"authorPointer"},
  3. "includefilter":{"author":{"fields":["id","name"]}}
  4. }
  5. $.ajax({
  6. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  7. "type": "GET",
  8. "cache": false,
  9. "headers": {
  10. "X-APICloud-AppId": "{{your_app_id}}",
  11. "X-APICloud-AppKey": "{{加密后的key}}"
  12. }
  13. }).done(function (data, status, header) {
  14. //success body
  15. }).fail(function (header, status, errorThrown) {
  16. //fail body
  17. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1,
  11. "authorPointer": {
  12. "name": "Member A",
  13. "id": 1
  14. }
  15. },
  16. {
  17. "title": "Post B",
  18. "id": 2,
  19. "memberId": 1,
  20. "authorPointer": {
  21. "name": "Member A",
  22. "id": 1
  23. }
  24. },
  25. {
  26. "title": "Post C",
  27. "id": 3,
  28. "memberId": 1,
  29. "authorPointer": {
  30. "name": "Member A",
  31. "id": 1
  32. }
  33. }
  34. ]
  35. },
  36. {
  37. "name": "Member B",
  38. "age": 22,
  39. "id": 2,
  40. "posts": [
  41. {
  42. "title": "Post D",
  43. "id": 4,
  44. "memberId": 2,
  45. "authorPointer": {
  46. "name": "Member B",
  47. "id": 2
  48. }
  49. }
  50. ]
  51. }, ... ]

REST 语法

  1. GET /api/members?filter[include][posts]=authorPointer&filter[where][age]=21&filter[includefilter][post][where][title]=Post A&filter[includefilter][author][fields]=id&filter[includefilter][author][fields]=name

Stringified 语法

  1. GET /api/members?filter={"include":{"posts":"authorPointer"},"where":{"age":21},"includefilter":{"post":{"where":{"title":"Post A"}},"author":{"fields":["id","name"]}}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "where":{"age":21},
  6. "include":{"posts":"authorPointer"},
  7. "includefilter":{
  8. "post":{"where":{"title":"Post A"}},
  9. "author":{"fields":["id","name"]}
  10. }
  11. }
  12. }, function(ret,err){
  13. if(err){
  14. //处理错误 err
  15. }else{
  16. //处理数据 ret
  17. }
  18. })

jquery.ajax

  1. var filter = {
  2. "where":{"age":21},
  3. "include":{"posts":"authorPointer"},
  4. "includefilter":{
  5. "post":{"where":{"title":"Post A"}},
  6. "author":{"fields":["id","name"]}
  7. }
  8. }
  9. $.ajax({
  10. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  11. "type": "GET",
  12. "cache": false,
  13. "headers": {
  14. "X-APICloud-AppId": "{{your_app_id}}",
  15. "X-APICloud-AppKey": "{{加密后的key}}"
  16. }
  17. }).done(function (data, status, header) {
  18. //success body
  19. }).fail(function (header, status, errorThrown) {
  20. //fail body
  21. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1,
  11. "authorPointer": {
  12. "name": "Member A",
  13. "id": 1
  14. }
  15. }
  16. ]
  17. }
  18. ]

REST 语法

  1. GET /mcm/api/members?filter[include][posts]=authorPointer&filter[limit]=2&filter[includefilter][post][limit]=2

Stringified 语法

  1. GET /mcm/api/members?filter={"include":{"posts":"authorPointer"},"limit":2,"includefilter":{"post":{"limit":2}}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":{"posts":"authorPointer"},
  6. "limit":2,
  7. "includefilter":{"post":{"limit":2}}
  8. }
  9. }, function(ret,err){
  10. if(err){
  11. //处理错误 err
  12. }else{
  13. //处理数据 ret
  14. }
  15. })

jquery.ajax

  1. var filter = {
  2. "include":{"posts":"authorPointer"},
  3. "limit":2,
  4. "includefilter":{"post":{"limit":2}}
  5. }
  6. $.ajax({
  7. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  8. "type": "GET",
  9. "cache": false,
  10. "headers": {
  11. "X-APICloud-AppId": "{{your_app_id}}",
  12. "X-APICloud-AppKey": "{{加密后的key}}"
  13. }
  14. }).done(function (data, status, header) {
  15. //success body
  16. }).fail(function (header, status, errorThrown) {
  17. //fail body
  18. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1,
  11. "authorPointer": {
  12. "name": "Member A",
  13. "age": 21,
  14. "id": 1
  15. }
  16. },
  17. {
  18. "title": "Post B",
  19. "id": 2,
  20. "memberId": 1,
  21. "authorPointer": {
  22. "name": "Member A",
  23. "age": 21,
  24. "id": 1
  25. }
  26. }
  27. ]
  28. },
  29. {
  30. "name": "Member B",
  31. "age": 22,
  32. "id": 2,
  33. "posts": [
  34. {
  35. "title": "Post D",
  36. "id": 4,
  37. "memberId": 2,
  38. "authorPointer": {
  39. "name": "Member B",
  40. "age": 22,
  41. "id": 2
  42. }
  43. }
  44. ]
  45. }
  46. ]

REST 语法

  1. GET /mcm/api/members?filter[include]=posts&filter[include]=passports&filter[limit]=5&filter[includefilter][post][limit]=1&filter[includefilter][passport][limit]=1

Stringified 语法

  1. GET /mcm/api/members?filter={"include":["posts","passports"],"limit":5,"includefilter":{post:{"limit":1},"passport":{"limit":1}}}

js-sdk

  1. var client = new Resource("appId", "appKey");
  2. var Model = client.Factory("Company");
  3. Model.query({
  4. filter:{
  5. "include":["posts","passports"],
  6. "limit":5,
  7. "includefilter":{
  8. post:{"limit":1},
  9. "passport":{"limit":1}
  10. }
  11. }
  12. }, function(ret,err){
  13. if(err){
  14. //处理错误 err
  15. }else{
  16. //处理数据 ret
  17. }
  18. })

jquery.ajax

  1. var filter = {
  2. "include":["posts","passports"],
  3. "limit":5,
  4. "includefilter":{
  5. post:{"limit":1},
  6. "passport":{"limit":1}
  7. }
  8. }
  9. $.ajax({
  10. "url": "https://d.apicloud.com/mcm/api/Company?filter=" + encodeURIComponent(JSON.stringify(filter)),
  11. "type": "GET",
  12. "cache": false,
  13. "headers": {
  14. "X-APICloud-AppId": "{{your_app_id}}",
  15. "X-APICloud-AppKey": "{{加密后的key}}"
  16. }
  17. }).done(function (data, status, header) {
  18. //success body
  19. }).fail(function (header, status, errorThrown) {
  20. //fail body
  21. })

Returns

  1. [
  2. {
  3. "name": "Member A",
  4. "age": 21,
  5. "id": 1,
  6. "posts": [
  7. {
  8. "title": "Post A",
  9. "id": 1,
  10. "memberId": 1
  11. }
  12. ],
  13. "passports": [
  14. {
  15. "number": "1",
  16. "id": 1,
  17. "ownerId": 1
  18. }
  19. ]
  20. },
  21. {
  22. "name": "Member B",
  23. "age": 22,
  24. "id": 2,
  25. "posts": [
  26. {
  27. "title": "Post D",
  28. "id": 4,
  29. "memberId": 2
  30. }
  31. ],
  32. "passports": [
  33. {
  34. "number": "2",
  35. "id": 2,
  36. "ownerId": 2
  37. }
  38. ]
  39. }, ... ]

错误代码清单

  • 100:”操作失败”,
  • 101:”应用不存在”,
  • 102:”数据云服务未开启,请在控制台的云开发中开启!”,
  • 103:”您没有应用访问权限”,
  • 104:”参数错误”,
  • 105:”Class不存在”,
  • 106:”Class名称错误”,
  • 107:”Class名称已经存在”,
  • 108:”连接数据库失败”,
  • 109:”内置Class不可删除”,
  • 110:”列字段已经存在”,
  • 111:”删除Class失败”,
  • 112:”删除数据失败”,
  • 113:”默认字段不可删除”,
  • 114:”删除列失败”,
  • 115:”导入数据文件不可为空”,
  • 116:”导入数据失败”,
  • 117:”导出数据失败”,
  • 118:”邮箱格式错误”,
  • 119:”邮箱验证未开启, 请在控制台的云开发中开启!”,
  • 120:”文件不存在”,
  • 121:”文件上传失败”,
  • 122:”保存文件失败”,
  • 123:”文件大小不能大于20M”
  • 124:”导出数据不存在”