leancloud javascript文档笔记

/ 0评 / 0
注意AV.Object.extend 产生的对象需要作为全局变量保存,因为每调用 一次,就会产生一个新的类的实例,并且和之前创建的实例形成一个链表。 如果你的应用时不时出现 Maximum call stack size exceeded 错误,请 确认是否误用了该方法。

这里要注意,我们每个存储条目的 objectId 是服务器端自动生成的唯一 id(非简单的自增逻辑生成),所以 objectId 是不可修改的。如果你有自定义 id 的需求,可以自己建立一个字段,逻辑上作为你的自定义 id。

另外,通过使用 increment(key, amount) 方法,你可以自行定义增减的幅度(amount 缺省值为 1)。

如果想在更新成功后获取最新的对象属性,请设置fetchWhenSave

 
+

 

 

数组

为了帮你存储数组类数据,LeanCloud 提供了三种操作让你可以原子地改动一个数组的值(当然,他们都需要一个给定的 key):

+

 

  • add: 在一个数组的末尾加入一个给定的对象
  • addUnique: 只会把原本不存在的对象加入数组,所以加入的位置没有保证
  • remove: 在一个数组中删除所有指定的实例
 
比如,我们想在一条微博的属性 "tags" 中加入多个属性值:
<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">addUnique</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"tags"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Frontend"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">addUnique</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"tags"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"JavaScript"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>
post.save();

你可以使用 unset 方法来删除一个实例中的单个属性:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 这样可以删掉 pubTimestamp 属性,本来也不必要,我们完全可以用 createdAt 属性代替它。</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">unset</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubTimestamp"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> <br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 写回 LeanCloud</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">save</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span>

批量删除一批对象可以这样:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Object</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">destroyAll</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">objects</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>
 
+
 
其中 objects 是一个对象集合,且其中的每个对象的 className 必须一样。



如果是现有对象想要关联到新对象,你同样可以通过只用它们的 objectId 来连接彼此。 请注意,不能直接像上面的例子那样将现有对象设置进去,而是必须 new一个新对象并只设置 objectId 属性:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Object</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">createWithoutData</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"5590cdfde4b00f7adb5860c8"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> myComment</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">set</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>
 
4

或者:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">id </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">'5590cdfde4b00f7adb5860c8'</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">;</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> myComment</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">set</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>
 
+

默认情况下,当获取一个对象时,关联的 AV.Object 不会被获取到,这些对象的值不能访问,除非像下面这样获取它们:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> fetchedComment</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">get</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">fetch</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">({</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />   success</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> content </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">get</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"content"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">});</span>

你可以在接下来关于 AV.Query 的章节中看到更详细的内容。一个 AV.Relation 的行为很像一个 AV.Object 数组,所以任何在数组可做的查询操作,也都可以作用在 AV.Relation 上。

+

 

如果你知道 post,想反向查询 user,可以通过 AV.Relation.reverseQuery 方法:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> query </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Relation</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">reverseQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">'_User'</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">'likes'</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">find</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">({</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />   success</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">users</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// users 是表示喜欢这个 post 的用户列表。</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">});</span>

AV.Object 实例的大小不应该超过 128 KB,如果需要存储较大的文件类型如图像、文件、音乐,可以使用 AV.File 对象来存储,具体使用方法可见 AV.File 指南部分。关于处理数据的更多信息,可查看开发指南的数据安全部分。

如果你需要查看一个 AV.User 是否已经认证过了,你可以调用 authenticated 方 法。你不需要查看一个认证方法中返回的 AV.User 对象是否已经通过验证了。
 

通过 AV.User._logInWith(platform, options) 来将微博、微信等第三方平台号绑定到 AV.User 上,例如:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">User</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">_logInWith</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"weibo"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"authData"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />      </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"uid"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"123456789"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />        </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"access_token"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"2.00vs3XtCI5FevCff4981adb5jj1lXE"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />        </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"expiration_in"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"36000"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">},</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />     success</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">user</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">){</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />        </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">//返回绑定后的用户</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />         console</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">dir</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">user</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">},</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />     error</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">err</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">){</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />        console</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">dir</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">err</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">})</span>


AV.ACL(user) 生成一个 AV.ACL 来限定 user 的访问。一个对象的 ACL 会在对象 保存的时候被存储起来,就像其他的属性一样。这样,为了创建一个当前 user 私有 的一个 note:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 该语句应该只声明一次</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Note</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Object</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">extend</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Note"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> privateNote </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Note</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> privateNote</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">set</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"content"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"This note is private!"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> privateNote</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">setACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">ACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">User</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">current</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">()));</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> privateNote</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">save</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span>
 
+

这个 note 只能由当前的用户所访问,但是对用户登录的设备没有限制,只要 是相同的用户就可以了。这项功能对于你如果想让用户再任何其他的设备上保存 和访问数据十分有用,比如说一个私人的 todo list 应用。

+

 

权限也能在使用者的基础上授予,你可以通过 setReadAccess 和 setWriteAccess 方 法独立的向 AV.ACL 中添加权限。比如,假设你有一条消息想要发送给一个组里 的多个用户,他们中的每一个都有读和写的权限:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 该语句应该只声明一次</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Message</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Object</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">extend</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Message"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> groupMessage </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Message</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> groupACL </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">ACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// userList is an array with the users we are sending this message to.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">for</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> i </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">0</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">;</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> i </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;"><</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> userList</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">length</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">;</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> i</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">++)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> groupACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">setReadAccess</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">userList</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">[</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">i</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">],</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">true</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> groupACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">setWriteAccess</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">userList</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">[</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">i</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">],</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">true</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> groupMessage</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">setACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">groupACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> groupMessage</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">save</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span>
 
+

你同样可以对所有的用户授权,只要使用 setPublicReadAccess 和 setPublicWriteAccess 就可以了。这样允许了在一个消息板上发评论的模式,比如我 们要创建一个 post 只能被它的作者修改,但是可以被所有人读取:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> publicPost </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> postACL </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">ACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">User</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">current</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">());</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> postACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">setPublicReadAccess</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">true</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> publicPost</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">setACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">postACL</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> publicPost</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">save</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span>


角色继承

就像上面所描述的一样,一个角色可能包含其他的角色,表示两个角色之间的父- 子关系,这样做的结果就是任何被授予一个角色的权限都会被隐式地授予这个角 色的所有子角色.

+

 

这样的关系很经常会在有用户管理内容的程序之中看到,比如论坛,有一个很少量 的用户称为管理员,有最高的权限,比如程序设定,创建新的论坛,设定所有人能看 到的内容等等.另一类有一部分类似于"版主"的用户,这些人有责任保持用户创建 的内容是合适的.任何一个"版主"有的权限"管理员"都应该有.为了启用这种关系,你 应该使"管理员"成为"版主"的一个子角色.

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> administrators </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">/* Your "Administrators" role */</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">;</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> moderators </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">/* Your "Moderators" role */</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">;</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> moderators</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">getRoles</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">().</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">add</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">administrators</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> moderators</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">save</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span>








find
first
equalTo
notEqualTo
greaterThan
lessThan
limit
skip
ascending
descending
lessThanOrEqualTo
greaterThanOrEqualTo
containedIn
notContainedIn
exists
doesNotExist
matchesKeyInQuery
doesNotMatchKey
select
containsAll
sizeEqualTo
startsWith
include
count
destroyAll
AV.Query.or






组合查询

如果你想要查找满足一系列查询的对象,你可以使用 AV.Query.or 方法来构建 查询,这样得到的结果是所有查询的并集。例如,你想查询出企业官方账号和个人账号的微博,可以这样:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> officialPosts </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> officialPosts</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">greaterThan</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubUserCertificate"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">2</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> individualPosts </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> individualPosts</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">lessThan</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubUserCertificate"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">2</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> mainQuery </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">or</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">officialPosts</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> individualPosts</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> mainQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">find</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">({</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> success</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">results</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// results 包含企业官方账号和个人账号发布的一些微博.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">},</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> error</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">error</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 失败了.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">});</span>
 
+

你也可以对 AV.Query 加入更多的条件,如同 AND 查询一样,这样得到所有查询结果的交集。

+

 

 
请注意我们不会在组合查询的子查询中支持非过滤型的条件(比如:limit, skip, ascending/descending, include)。


对于超过 1000 个对象的类来说,count 操作会被时间限制所约束,它们可能会一直 返回超时错误,或者只是返回一个近似正确的值。这样的话你应该更合理地规划你 程序的结构来避免这种情况。

对字符串类型做查询

使用 startWith 来限制属性值以一个特定的字符串开头,这和 MySQL 的 LIKE 操作 符很像,因为有索引所以对于大的数据集这个操作也是很高效的。

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 找出名字以 "LeanCloud" 开头的账户的微博帖子</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> query </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">startsWith</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubUser"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"LeanCloud"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>

对数组值做查询

对于属性值是数组的情况,你可以这样查询数组的值中有 2 的实例列表:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">equalTo</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"arrayKey"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">2</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>
 
+

你同样可以用下面的方式找到属性值中同时包含元素 2,3,4 的实例列表:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">containsAll</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"arrayKey"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">[</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">2</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">3</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">4</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">]);</span>
 
+

此外,你还可以根据数组长度来查询,比如查询 arrayKey 的长度为 3 的对象列表:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">sizeEqualTo</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">'arrayKey'</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #2aa198;">3</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>

剩下的字段可以之后用返回的对象的 fetch 方法来获取:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">first</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">().</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">then</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">result</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 这里只会返回指定的属性,再次获取全部属性.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">return</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> result</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">fetch</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">();</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}).</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">then</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">result</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 这里会返回所有属性.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">});</span>

如果想让返回的对象的某个属性匹配多个值,你可以使用 containedIn,提供一个数组就可以了。这样通常可以用单个的查询来获取多个结果,比如 你要查询 「LeanCloud官方客服」,「LeanCloud江宏」,「滚滚艾买提」 三个账号的微博时,可以使用 containedIn(类似 SQL 中的 in 查询)方法来实现:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">containedIn</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubUser"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />                  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">[</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"LeanCloud官方客服"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"LeanCloud江宏"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"滚滚艾买提"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">]);</span>
 
相反地,你可以使用 notContainedIn 方法来查询在集合之外的目标对象。

你可以使用 matchesKeyInQuery 方法来进行嵌套的子查询。例如,在微博这类应用中有三类数据:一个类是微博帖子信息(Post),另一个类是用户账户信息(AVUser),还有一个类是用户之间互相关注的信息(UserFollow),要找出当前用户关注的人发布的微博,则:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 该语句应该只声明一次</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">UserFollow</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Object</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">extend</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"UserFollow"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Object</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">extend</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"Post"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 先找到当前登录用户关注的用户列表</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> userQuery </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">UserFollow</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> userQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">equalTo</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"follower"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">User</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">current</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">());</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 找到这些被关注者发布的微博</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> postQuery </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> postQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">matchesKeyInQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"author"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"followee"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> userQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> postQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">find</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">({</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> success</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">results</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 得到当前用户关注的人发布的微博</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">});</span>
 
+

相反,要从一个查询中获取一组对象,该对象的一个键值,与另一个对象的键值并不匹配,可以使用 doesNotMatchKeyInQuery 。 例如,找出当前用户没有关注的人发布的微博:

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">var</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> postQuery </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">=</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">new</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> AV</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #b58900;">Post</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> postQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">doesNotMatchKeyInQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"author"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"followee"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">,</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> userQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> postQuery</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">find</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">({</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />   success</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">:</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #859900;">function</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">results</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">)</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"> </span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">{</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />    </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 得到非当前用户关注的人发布的微博</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br />  </span> <span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">}</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">});</span>
+

 

如果你想要查询含有某一特定属性的对象,你可以使用 exists。相对地,如果你想获 取没有这一特定属性的对象,你可以使用 doesNotExist

+

 

<span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 查找含有 pubTimestamp 属性的微博</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">exists</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubTimestamp"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> <br /></span><span style="box-sizing: border-box; -webkit-user-select: text; color: #586175; font-style: italic;">// 查找不含有 pubTimestamp 属性的微博</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;"><br /> query</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">.</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #bd3613;">doesNotExist</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">(</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #269186;">"pubTimestamp"</span><span style="box-sizing: border-box; -webkit-user-select: text; color: #839496;">);</span>

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注