李烈火 發表於 2019-8-19 16:07:00

laravel操作mongo详细说明

<div class="bs-callout bs-callout-default">原文地址:http://returnc.com/detail/3728</div>
<div class="bs-callout bs-callout-default">&nbsp;</div>
<div class="bs-callout bs-callout-default">一个Eloquent模型和Query构建器,支持MongoDB,使用原始的Laravel API。该库扩展了原始的Laravel类,因此它使用完全相同的方法。</div>
<div class="post_content">
<p>目录</p>
<ul>
<li>安装 Installation</li>
<li>升级 Upgrading</li>
<li>配置 Configuration</li>
<li>Eloquent</li>
<li>可选: Alias</li>
<li>查询器 Query Builder</li>
<li>构建器 Schema</li>
<li>Extensions</li>
<li>样例 Examples</li>
</ul>
<h3 id="installation" class="pos_0">安装&nbsp;Installation</h3>
<p>确保安装了MongoDB PHP驱动程序。您可以在&nbsp;http://php.net/manual/en/mongodb.installation.php找到安装说明</p>
<p>警告:版本&gt; = 3.0时不再支持旧的mongo PHP驱动程序。</p>
<p>使用composer安装:</p>
<pre><code class="hljs coffeescript">composer <span class="hljs-built_in">require jenssegers/mongodb</span></code></pre>
<h3 class="pos_1">Laravel版兼容性</h3>
<table class="table">
<thead>
<tr><th align="left">Laravel</th><th align="left">Package</th></tr>
</thead>
<tbody>
<tr>
<td align="left">4.2.x</td>
<td align="left">2.0.x</td>
</tr>
<tr>
<td align="left">5.0.x</td>
<td align="left">2.1.x</td>
</tr>
<tr>
<td align="left">5.1.x</td>
<td align="left">2.2.x or 3.0.x</td>
</tr>
<tr>
<td align="left">5.2.x</td>
<td align="left">2.3.x or 3.0.x</td>
</tr>
<tr>
<td align="left">5.3.x</td>
<td align="left">3.1.x or 3.2.x</td>
</tr>
<tr>
<td align="left">5.4.x</td>
<td align="left">3.2.x</td>
</tr>
<tr>
<td align="left">5.5.x</td>
<td align="left">3.3.x</td>
</tr>
<tr>
<td align="left">5.6.x</td>
<td align="left">3.4.x</td>
</tr>
</tbody>
</table>
<p>并添加服务提供商<code>config/app.php</code>:</p>
<pre><code class="hljs ruby">Jenssegers\Mongodb\MongodbServiceProvider::<span class="hljs-class"><span class="hljs-keyword">class,</span></span></code></pre>
<p>要与Lumen一起使用,请添加服务提供商<code>bootstrap/app.php</code>。在此文件中,您还需要启用Eloquent。不过,你必须确保你的调用<code>$app-&gt;withEloquent();</code>是下面您已经注册的,其中<code>MongodbServiceProvider</code>:</p>
<pre><code class="hljs php">$app-&gt;register(Jenssegers\Mongodb\MongodbServiceProvider::class);

$app-&gt;withEloquent();</code></pre>
<p>服务提供者将使用原始数据库管理器注册mongodb数据库扩展。无需注册其他外墙或物体。使用mongodb连接时,Laravel会自动为您提供相应的mongodb对象。</p>
<p>要在Laravel外使用,请查看Capsule管理器并添加:</p>
<pre><code class="hljs php">$capsule-&gt;getDatabaseManager()-&gt;extend(<span class="hljs-string">'mongodb', <span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($config)
{
    <span class="hljs-keyword">return <span class="hljs-keyword">new Jenssegers\Mongodb\Connection($config);
});</span></span></span></span></span></span></code></pre>
<h3 id="upgrading" class="pos_2">升级&nbsp;Upgrading</h3>
<h4>从版本2升级到3</h4>
<p>在这个支持新mongodb PHP扩展的新主要版本中,我们还移动了Model类的位置,并用特征替换了MySQL模型类。</p>
<p>请更改所有模型顶部或注册别名<code>Jenssegers\Mongodb\Model</code><code>为</code><code>Jenssegers\Mongodb\Eloquent\Model</code>。</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {}</span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>如果您使用混合关系,您的MySQL类现在应该扩展原始的Eloquent模型类<code>Illuminate\Database\Eloquent\Model</code>不是删除<code>Jenssegers\Eloquent\Model</code>。而是使用新的<code>Jenssegers\Mongodb\Eloquent\HybridRelations</code>特性。这应该使事情更清楚,因为此包中只有一个单一的模型类。</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">HybridRelations;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">use <span class="hljs-title">HybridRelations;

    <span class="hljs-keyword">protected $connection = <span class="hljs-string">'mysql';

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>嵌入式关系现在返回一个<code>Illuminate\Database\Eloquent\Collection</code>而不是自定义Collection类。如果您使用的是一种可用的特殊方法,请将它们转换为Collection集合操作。</p>
<pre><code class="hljs php">$books = $user-&gt;books()-&gt;sortBy(<span class="hljs-string">'title');</span></code></pre>
<h3 class="pos_3">测试&nbsp;Testing</h3>
<p>要运行此程序包的测试,请运行:</p>
<pre><code class="hljs"><code>docker-compose up
</code></code></pre>
<h3 id="configuration" class="pos_4">配置 Configuration</h3>
<p>更改您的默认数据库连接名称<code>config/database.php</code>:</p>
<pre><code class="hljs php"><span class="hljs-string">'default' =&gt; env(<span class="hljs-string">'DB_CONNECTION', <span class="hljs-string">'mongodb'),</span></span></span></code></pre>
<p>添加一个新的mongodb连接:</p>
<pre><code class="hljs php"><span class="hljs-string">'mongodb' =&gt; [
    <span class="hljs-string">'driver'   =&gt; <span class="hljs-string">'mongodb',
    <span class="hljs-string">'host'   =&gt; env(<span class="hljs-string">'DB_HOST', <span class="hljs-string">'localhost'),
    <span class="hljs-string">'port'   =&gt; env(<span class="hljs-string">'DB_PORT', <span class="hljs-number">27017),
    <span class="hljs-string">'database' =&gt; env(<span class="hljs-string">'DB_DATABASE'),
    <span class="hljs-string">'username' =&gt; env(<span class="hljs-string">'DB_USERNAME'),
    <span class="hljs-string">'password' =&gt; env(<span class="hljs-string">'DB_PASSWORD'),
    <span class="hljs-string">'options'=&gt; [
      <span class="hljs-string">'database' =&gt; <span class="hljs-string">'admin' <span class="hljs-comment">// sets the authentication database required by mongo 3
    ]
],</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>您可以使用以下配置连接到多个服务器或副本集:</p>
<pre><code class="hljs php"><span class="hljs-string">'mongodb' =&gt; [
    <span class="hljs-string">'driver'   =&gt; <span class="hljs-string">'mongodb',
    <span class="hljs-string">'host'   =&gt; [<span class="hljs-string">'server1', <span class="hljs-string">'server2'],
    <span class="hljs-string">'port'   =&gt; env(<span class="hljs-string">'DB_PORT', <span class="hljs-number">27017),
    <span class="hljs-string">'database' =&gt; env(<span class="hljs-string">'DB_DATABASE'),
    <span class="hljs-string">'username' =&gt; env(<span class="hljs-string">'DB_USERNAME'),
    <span class="hljs-string">'password' =&gt; env(<span class="hljs-string">'DB_PASSWORD'),
    <span class="hljs-string">'options'=&gt; [
                <span class="hljs-string">'replicaSet' =&gt; <span class="hljs-string">'replicaSetName'
        ]
],</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>或者,您可以使用MongoDB连接字符串:</p>
<pre><code class="hljs php"><span class="hljs-string">'mongodb' =&gt; [
    <span class="hljs-string">'driver'   =&gt; <span class="hljs-string">'mongodb',
    <span class="hljs-string">'dsn' =&gt; env(<span class="hljs-string">'DB_DSN'),
    <span class="hljs-string">'database' =&gt; env(<span class="hljs-string">'DB_DATABASE'),
],</span></span></span></span></span></span></span></code></pre>
<p>有关其URI格式,请参阅MongoDB官方文档:https://docs.mongodb.com/manual/reference/connection-string/</p>
<h3 class="pos_5">Eloquent</h3>
<p>该软件包包括一个启用MongoDB的Eloquent类,您可以使用它来定义相应集合的模型。</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {}</span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>请注意,我们没有告诉Eloquent哪个集合用于<code>User</code>模型。就像原始的Eloquent一样,除非明确指定其他名称,否则该类的小写复数名称将用作集合名称。您可以通过<code>collection</code>在模型上定义属性来指定自定义集合(表的别名):</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">protected $collection = <span class="hljs-string">'users_collection';

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>注意:&nbsp;Eloquent还假定每个集合都有一个名为id的主键列。您可以定义一个<code>primaryKey</code>属性来覆盖此约定。同样,您可以定义一个<code>connection</code>属性来覆盖使用模型时应该使用的数据库连接的名称。</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">MyModel <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">protected $connection = <span class="hljs-string">'mongodb';

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>其他所有(应该)都像原始的Eloquent模型一样工作。在http://laravel.com/docs/eloquent上阅读有关Eloquent的更多信息</p>
<h3 id="optional-alias" class="pos_6">可选项:别名&nbsp;Alias</h3>
<p>您还可以通过将以下内容添加到别名数组中来注册MongoDB模型的别名<code>config/app.php</code>:</p>
<p>'Moloquent' =&gt; Jenssegers\Mongodb\Eloquent\Model::class,</p>
<p>这将允许您使用注册的别名,如:</p>
<pre><code class="hljs coffeescript"><span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">MyModel <span class="hljs-keyword">extends <span class="hljs-title">Moloquent {}</span></span></span></span></span></code></pre>
<h3 id="query-builder" class="pos_7">查询生成器&nbsp;Query Builder</h3>
<p>数据库驱动程序直接插入原始查询构建器。使用mongodb连接时,您将能够构建流畅的查询来执行数据库操作。为方便起见,还有一个<code>collection</code>别名<code>table</code>以及一些额外的mongodb特定操作符/操作。</p>
<pre><code class="hljs php">$users = DB::collection(<span class="hljs-string">'users')-&gt;get();

$user = DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;first();</span></span></span></span></code></pre>
<p>如果未更改默认数据库连接,则需要在查询时指定它。</p>
<pre><code class="hljs php">$user = DB::connection(<span class="hljs-string">'mongodb')-&gt;collection(<span class="hljs-string">'users')-&gt;get();</span></span></code></pre>
<p>在http://laravel.com/docs/queries上阅读有关查询构建器的更多信息</p>
<h3 id="schema" class="pos_8">构建器&nbsp;Schema</h3>
<p>数据库驱动程序还具有(有限的)架构构建器支持。您可以轻松地操作集合并设置索引:</p>
<pre><code class="hljs php">Schema::create(<span class="hljs-string">'users', <span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($collection)
{
    $collection-&gt;index(<span class="hljs-string">'name');

    $collection-&gt;unique(<span class="hljs-string">'email');
});</span></span></span></span></span></span></code></pre>
<p>支持的操作如下:</p>
<ul>
<li>create 和 drop</li>
<li>collection</li>
<li>hasCollection</li>
<li>index 和 dropIndex (compound indexes supported as well)</li>
<li>unique</li>
<li>background, sparse, expire, geospatial (MongoDB specific)</li>
</ul>
<p>所有其他(不支持的)操作都实现为虚拟传递方法,因为MongoDB不使用预定义的模式。在http://laravel.com/docs/schema上阅读有关架构构建器的更多信息</p>
<p>&nbsp;</p>
<h3 class="pos_9">地理空间索引</h3>
<p>2dsphere。使用模式构建器将这些添加到集合中。</p>
<pre><code class="hljs php">Schema::create(<span class="hljs-string">'users', <span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($collection)
{
    $collection-&gt;geospatial(<span class="hljs-string">'name', <span class="hljs-string">'2d');
});</span></span></span></span></span></span></code></pre>
<p>添加&nbsp;<code>2dsphere</code>&nbsp;index:</p>
<pre><code class="hljs php">Schema::create(<span class="hljs-string">'users', <span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($collection)
{
    $collection-&gt;geospatial(<span class="hljs-string">'name', <span class="hljs-string">'2dsphere');
});</span></span></span></span></span></span></code></pre>
<h3 id="extensions" class="pos_10">扩展&nbsp;Extensions</h3>
<h3 class="pos_11">认证</h3>
<p>如果您想使用Laravel的本机Auth功能,请注册此包含的服务提供商:</p>
<pre><code class="hljs bash"><span class="hljs-string">'Jenssegers\Mongodb\Auth\PasswordResetServiceProvider',</span></code></pre>
<p>此服务提供程序将稍微修改内部DatabaseReminderRepository以添加对基于MongoDB的密码提醒的支持。如果您不使用密码提醒,则无需注册此服务提供商,其他一切都应该正常工作。</p>
<h3 class="pos_12">队列</h3>
<p>如果要将MongoDB用作数据库后端,请更改以下驱动程序<code>config/queue.php</code>:</p>
<pre><code class="hljs php"><span class="hljs-string">'connections' =&gt; [
    <span class="hljs-string">'database' =&gt; [
      <span class="hljs-string">'driver' =&gt; <span class="hljs-string">'mongodb',
      <span class="hljs-string">'table'=&gt; <span class="hljs-string">'jobs',
      <span class="hljs-string">'queue'=&gt; <span class="hljs-string">'default',
      <span class="hljs-string">'expire' =&gt; <span class="hljs-number">60,
    ],</span></span></span></span></span></span></span></span></span></span></code></pre>
<p>如果要使用MongoDB处理失败的处理,请在<code>config/queue.php</code>以下位置更改数据库:</p>
<pre><code class="hljs php"><span class="hljs-string">'failed' =&gt; [
    <span class="hljs-string">'database' =&gt; <span class="hljs-string">'mongodb',
    <span class="hljs-string">'table'    =&gt; <span class="hljs-string">'failed_jobs',
    ],</span></span></span></span></span></code></pre>
<p>并添加服务提供商<code>config/app.php</code>:</p>
<pre><code class="hljs ruby">Jenssegers\Mongodb\MongodbQueueServiceProvider::<span class="hljs-class"><span class="hljs-keyword">class,</span></span></code></pre>
<h3 class="pos_13">Sentry</h3>
<p>如果您想将此库与https://github.com/jenssegers/Laravel-MongoDB-Sentry</p>
<h3 class="pos_14">Sessions</h3>
<p>MongoDB会话驱动程序在单独的包中提供,请查看https://github.com/jenssegers/Laravel-MongoDB-Session</p>
<h3 id="examples" class="pos_15">例子&nbsp;Examples</h3>
<h4>基本用法</h4>
<p>检索所有模型</p>
<pre><code class="hljs php">$users = User::all();</code></pre>
<p>通过主键检索记录</p>
<pre><code class="hljs php">$user = User::find(<span class="hljs-string">'517c43667db388101e00000f');</span></code></pre>
<p>Wheres</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'votes', <span class="hljs-string">'&gt;', <span class="hljs-number">100)-&gt;take(<span class="hljs-number">10)-&gt;get();</span></span></span></span></code></pre>
<p>Or Statements</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'votes', <span class="hljs-string">'&gt;', <span class="hljs-number">100)-&gt;orWhere(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;get();</span></span></span></span></span></code></pre>
<p>And Statements</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'votes', <span class="hljs-string">'&gt;', <span class="hljs-number">100)-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'=', <span class="hljs-string">'John')-&gt;get();</span></span></span></span></span></span></code></pre>
<p>Using Where In With An Array</p>
<pre><code class="hljs php">$users = User::whereIn(<span class="hljs-string">'age', [<span class="hljs-number">16, <span class="hljs-number">18, <span class="hljs-number">20])-&gt;get();</span></span></span></span></code></pre>
<p><code>whereNotIn</code>如果字段不存在,将返回使用对象。结合使用<code>whereNotNull('age')</code>可以省去这些文件。</p>
<p>Using Where Between</p>
<pre><code class="hljs php">$users = User::whereBetween(<span class="hljs-string">'votes', [<span class="hljs-number">1, <span class="hljs-number">100])-&gt;get();</span></span></span></code></pre>
<p>Where null</p>
<pre><code class="hljs php">$users = User::whereNull(<span class="hljs-string">'updated_at')-&gt;get();</span></code></pre>
<p>Order By</p>
<pre><code class="hljs php">$users = User::orderBy(<span class="hljs-string">'name', <span class="hljs-string">'desc')-&gt;get();</span></span></code></pre>
<p>Offset &amp; Limit</p>
<pre><code class="hljs php">$users = User::skip(<span class="hljs-number">10)-&gt;take(<span class="hljs-number">5)-&gt;get();</span></span></code></pre>
<p>Distinct</p>
<p>Distinct需要一个字段,可以返回不同的值。</p>
<pre><code class="hljs php">$users = User::distinct()-&gt;get([<span class="hljs-string">'name']);
<span class="hljs-comment">// or
$users = User::distinct(<span class="hljs-string">'name')-&gt;get();</span></span></span></code></pre>
<p>Distinct 结合&nbsp;where&nbsp;条件</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'active', <span class="hljs-keyword">true)-&gt;distinct(<span class="hljs-string">'name')-&gt;get();</span></span></span></code></pre>
<p>自定义 Wheres</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'name', <span class="hljs-string">'=', <span class="hljs-string">'John')-&gt;orWhere(<span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($query)
    {
      $query-&gt;where(<span class="hljs-string">'votes', <span class="hljs-string">'&gt;', <span class="hljs-number">100)
            -&gt;where(<span class="hljs-string">'title', <span class="hljs-string">'&lt;&gt;', <span class="hljs-string">'Admin');
    })
    -&gt;get();</span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>Group By</p>
<pre><code class="hljs php">$users = Users::groupBy(<span class="hljs-string">'title')-&gt;get([<span class="hljs-string">'title', <span class="hljs-string">'name']);</span></span></span></code></pre>
<p>聚合查询&nbsp;Aggregation</p>
<p><em>聚合仅适用于大于2.2的MongoDB版本</em><em>。</em></p>
<pre><code class="hljs php">$total = Order::count();
$price = Order::max(<span class="hljs-string">'price');
$price = Order::min(<span class="hljs-string">'price');
$price = Order::avg(<span class="hljs-string">'price');
$total = Order::sum(<span class="hljs-string">'price');</span></span></span></span></code></pre>
<p>聚合查询与&nbsp;where: 条件</p>
<pre><code class="hljs php">$sold = Orders::where(<span class="hljs-string">'sold', <span class="hljs-keyword">true)-&gt;sum(<span class="hljs-string">'price');</span></span></span></code></pre>
<p>聚合也可以用于:</p>
<pre><code class="hljs php">$total = Order::max(<span class="hljs-string">'suborder.price');
...</span></code></pre>
<p>注意:此aggreagtion仅适用于单个子文档(如embedsOne)而非子文档数组(如embedsMany)</p>
<p>Like</p>
<pre><code class="hljs sql">$user = <span class="hljs-keyword">Comment::<span class="hljs-keyword">where(<span class="hljs-string">'body', <span class="hljs-string">'like', <span class="hljs-string">'%spam%')-&gt;<span class="hljs-keyword">get();</span></span></span></span></span></span></code></pre>
<p>递增或递减列的值</p>
<p>对指定的属性执行增量或减量(默认值1):</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'name', <span class="hljs-string">'John Doe')-&gt;increment(<span class="hljs-string">'age');
User::where(<span class="hljs-string">'name', <span class="hljs-string">'Jaques')-&gt;decrement(<span class="hljs-string">'weight', <span class="hljs-number">50);</span></span></span></span></span></span></span></code></pre>
<p>返回更新的对象数:</p>
<pre><code class="hljs php">$count = User-&gt;increment(<span class="hljs-string">'age');</span></code></pre>
<p>您还可以指定要更新的其他列:</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'age', <span class="hljs-string">'29')-&gt;increment(<span class="hljs-string">'age', <span class="hljs-number">1, [<span class="hljs-string">'group' =&gt; <span class="hljs-string">'thirty something']);
User::where(<span class="hljs-string">'bmi', <span class="hljs-number">30)-&gt;decrement(<span class="hljs-string">'bmi', <span class="hljs-number">1, [<span class="hljs-string">'category' =&gt; <span class="hljs-string">'overweight']);</span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>软删除</p>
<p>软删除模型时,实际上并未从数据库中删除它。而是在记录上设置deleted_at时间戳。要为模型启用软删除,请将SoftDeletingTrait应用于模型:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">SoftDeletes;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">use <span class="hljs-title">SoftDeletes;

    <span class="hljs-keyword">protected $dates = [<span class="hljs-string">'deleted_at'];

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>有关更多信息,请访问http://laravel.com/docs/eloquent#soft-deleting</p>
<h3 class="pos_16">MongoDB特殊运算</h3>
<p>exists</p>
<p>匹配具有指定字段的文档。</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'age', <span class="hljs-string">'exists', <span class="hljs-keyword">true)-&gt;get();</span></span></span></code></pre>
<p>All</p>
<p>匹配包含查询中指定的所有元素的数组。</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'roles', <span class="hljs-string">'all', [<span class="hljs-string">'moderator', <span class="hljs-string">'author'])-&gt;get();</span></span></span></span></code></pre>
<p>Size</p>
<p>如果数组字段是指定大小,则选择文档。</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'tags', <span class="hljs-string">'size', <span class="hljs-number">3)-&gt;get();</span></span></span></code></pre>
<p>Regex</p>
<p>选择值与指定正则表达式匹配的文档。</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'name', <span class="hljs-string">'regex', <span class="hljs-keyword">new \MongoDB\BSON\Regex(<span class="hljs-string">"/.*doe/i"))-&gt;get();</span></span></span></span></code></pre>
<p>注意:您还可以使用Laravel regexp操作。这些更灵活,会自动将正则表达式字符串转换为MongoDB \ BSON \ Regex对象。</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'name', <span class="hljs-string">'regexp', <span class="hljs-string">'/.*doe/i'))-&gt;get();</span></span></span></code></pre>
<p>反之:</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'name', <span class="hljs-string">'not regexp', <span class="hljs-string">'/.*doe/i'))-&gt;get();</span></span></span></code></pre>
<p>Type</p>
<p>如果字段是指定类型,则选择文档。有关更多信息,请访问:http://docs.mongodb.org/manual/reference/operator/query/type/#op._S_type</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'age', <span class="hljs-string">'type', <span class="hljs-number">2)-&gt;get();</span></span></span></code></pre>
<p>Mod</p>
<p>对字段的值执行模运算,并选择具有指定结果的文档。</p>
<pre><code class="hljs php">User::where(<span class="hljs-string">'age', <span class="hljs-string">'mod', [<span class="hljs-number">10, <span class="hljs-number">0])-&gt;get();</span></span></span></span></code></pre>
<p>Near</p>
<p>注意:按此顺序指定坐标:<code>longitude, latitude</code>。</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'location', <span class="hljs-string">'near', [
        <span class="hljs-string">'$geometry' =&gt; [
      <span class="hljs-string">'type' =&gt; <span class="hljs-string">'Point',
          <span class="hljs-string">'coordinates' =&gt; [
                <span class="hljs-number">-0.1367563,
            <span class="hljs-number">51.5100913,
      ],
    ],
    <span class="hljs-string">'$maxDistance' =&gt; <span class="hljs-number">50,
]);</span></span></span></span></span></span></span></span></span></span></code></pre>
<p>GeoWithin</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'location', <span class="hljs-string">'geoWithin', [
        <span class="hljs-string">'$geometry' =&gt; [
      <span class="hljs-string">'type' =&gt; <span class="hljs-string">'Polygon',
          <span class="hljs-string">'coordinates' =&gt; [[
            [
                <span class="hljs-number">-0.1450383,
                <span class="hljs-number">51.5069158,
            ],      
            [
                <span class="hljs-number">-0.1367563,
                <span class="hljs-number">51.5100913,
            ],      
            [
                <span class="hljs-number">-0.1270247,
                <span class="hljs-number">51.5013233,
            ],
            [
                <span class="hljs-number">-0.1450383,
                <span class="hljs-number">51.5069158,
            ],
      ]],
    ],
]);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>GeoIntersects</p>
<pre><code class="hljs php">$locations = Location::where(<span class="hljs-string">'location', <span class="hljs-string">'geoIntersects', [
    <span class="hljs-string">'$geometry' =&gt; [
      <span class="hljs-string">'type' =&gt; <span class="hljs-string">'LineString',
      <span class="hljs-string">'coordinates' =&gt; [
            [
                <span class="hljs-number">-0.144044,
                <span class="hljs-number">51.515215,
            ],
            [
                <span class="hljs-number">-0.129545,
                <span class="hljs-number">51.507864,
            ],
      ],
    ],
]);</span></span></span></span></span></span></span></span></span></span></code></pre>
<p>Where</p>
<p>匹配满足JavaScript表达式的文档。有关更多信息,请访问http://docs.mongodb.org/manual/reference/operator/query/where/#op._S_where</p>
<h3 class="pos_17">插入,更新和删除</h3>
<p>插入,更新和删除记录就像原始的Eloquent一样。</p>
<p>Saving a new model</p>
<pre><code class="hljs php">$user = <span class="hljs-keyword">new User;
$user-&gt;name = <span class="hljs-string">'John';
$user-&gt;save();</span></span></code></pre>
<p>您还可以使用create方法将新模型保存在一行中:</p>
<pre><code class="hljs php">User::create([<span class="hljs-string">'name' =&gt; <span class="hljs-string">'John']);</span></span></code></pre>
<p>Updating a model</p>
<p>要更新模型,您可以检索它,更改属性并使用save方法。</p>
<pre><code class="hljs php">$user = User::first();
$user-&gt;email = <span class="hljs-string">'john@foo.com';
$user-&gt;save();</span></code></pre>
<p><em>还有对upsert操作的支持,请查看https://github.com/jenssegers/laravel-mongodb#mongodb-specific-operations</em></p>
<p>Deleting a model</p>
<p>要删除模型,只需在实例上调用delete方法:</p>
<pre><code class="hljs php">$user = User::first();
$user-&gt;delete();</code></pre>
<p>或者按键删除模型:</p>
<pre><code class="hljs php">User::destroy(<span class="hljs-string">'517c43667db388101e00000f');</span></code></pre>
<p>有关模型操作的更多信息,请查看http://laravel.com/docs/eloquent#insert-update-delete</p>
<h3 class="pos_18">Dates</h3>
<p>Eloquent允许您使用Carbon / DateTime对象而不是MongoDate对象。在内部,这些日期在保存到数据库时将转换为MongoDate对象。如果您希望在非默认日期字段上使用此功能,则需要按照此处所述手动指定它们:http://laravel.com/docs/eloquent#date-mutators</p>
<p>例:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">protected $dates = [<span class="hljs-string">'birthday'];

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>这允许您执行以下查询:</p>
<pre><code class="hljs php">$users = User::where(<span class="hljs-string">'birthday', <span class="hljs-string">'&gt;', <span class="hljs-keyword">new DateTime(<span class="hljs-string">'-18 years'))-&gt;get();</span></span></span></span></code></pre>
<h3 class="pos_19">Relations</h3>
<h3 class="pos_20">支持的关系是:</h3>
<ul>
<li>hasOne</li>
<li>hasMany</li>
<li>belongsTo</li>
<li>belongsToMany</li>
<li>embedsOne</li>
<li>embedsMany</li>
</ul>
<p>例:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">items<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;hasMany(<span class="hljs-string">'Item');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>归属关系:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Item <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">user<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;belongsTo(<span class="hljs-string">'User');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>belongsToMany关系不会使用数据透视表“table”,而是将id推送到related_ids属性。这使belongsToMany方法的第二个参数无用。如果要为关系定义自定义键,请将其设置为<code>null</code>:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">groups<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;belongsToMany(<span class="hljs-string">'Group', <span class="hljs-keyword">null, <span class="hljs-string">'user_ids', <span class="hljs-string">'group_ids');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>其他关系尚未得到支持,但可能会在未来添加。有关这些关系的更多信息,请访问http://laravel.com/docs/eloquent#relationships</p>
<h3 class="pos_21">嵌入许多关系</h3>
<p>如果要嵌入模型而不是引用模型,可以使用<code>embedsMany</code>关系。此关系类似于<code>hasMany</code>关系,但将模型嵌入父对象中。</p>
<p>记住:这些关系返回El​​oquent集合,它们不返回查询构建器对象!</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">books<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;embedsMany(<span class="hljs-string">'Book');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>您可以通过动态属性访问嵌入式模型:</p>
<pre><code class="hljs php">$books = User::first()-&gt;books;</code></pre>
<p>反向关系是自动神奇可用的,您不需要定义此归属关系。</p>
<pre><code class="hljs php">$user = $book-&gt;user;</code></pre>
<p>插入和更新嵌入式模型的工作方式类似于以下<code>hasMany</code>关系:</p>
<pre><code class="hljs php">$book = <span class="hljs-keyword">new Book([<span class="hljs-string">'title' =&gt; <span class="hljs-string">'A Game of Thrones']);

$user = User::first();

$book = $user-&gt;books()-&gt;save($book);
<span class="hljs-comment">// or
$book = $user-&gt;books()-&gt;create([<span class="hljs-string">'title' =&gt; <span class="hljs-string">'A Game of Thrones'])</span></span></span></span></span></span></code></pre>
<p>您可以使用他们的<code>save</code>方法更新嵌入式模型(从2.0.0版开始提供):</p>
<pre><code class="hljs php">$book = $user-&gt;books()-&gt;first();

$book-&gt;title = <span class="hljs-string">'A Game of Thrones';

$book-&gt;save();</span></code></pre>
<p>您可以使用<code>destroy</code>关系上的<code>delete</code>方法或模型上的方法(从2.0.0版开始提供)来删除嵌入式模型:</p>
<pre><code class="hljs php">$book = $user-&gt;books()-&gt;first();

$book-&gt;delete();
<span class="hljs-comment">// or
$user-&gt;books()-&gt;destroy($book);</span></code></pre>
<p>如果要在不触及数据库的情况下添加或删除嵌入式模型,可以使用<code>associate</code>和<code>dissociate</code>方法。要最终将更改写入数据库,请保存父对象:</p>
<pre><code class="hljs php">$user-&gt;books()-&gt;associate($book);

$user-&gt;save();</code></pre>
<p>与其他关系一样,embedsMany根据模型名称假定关系的本地键。您可以通过将第二个参数传递给embedsMany方法来覆盖默认本地键:</p>
<pre><code class="hljs php"><span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;embedsMany(<span class="hljs-string">'Book', <span class="hljs-string">'local_key');</span></span></span></span></code></pre>
<p>嵌入式关系将返回嵌入项的集合,而不是查询构建器。查看可用的操作:https://laravel.com/docs/master/collections</p>
<h3 class="pos_22">EmbedsOne Relations</h3>
<p>embedsOne关系类似于EmbedsMany关系,但只嵌入单个模型。</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Book <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">author<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;embedsOne(<span class="hljs-string">'Author');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>您可以通过动态属性访问嵌入式模型:</p>
<pre><code class="hljs php">$author = Book::first()-&gt;author;</code></pre>
<p>插入和更新嵌入式模型的工作方式类似于以下<code>hasOne</code>关系:</p>
<pre><code class="hljs php">$author = <span class="hljs-keyword">new Author([<span class="hljs-string">'name' =&gt; <span class="hljs-string">'John Doe']);

$book = Books::first();

$author = $book-&gt;author()-&gt;save($author);
<span class="hljs-comment">// or
$author = $book-&gt;author()-&gt;create([<span class="hljs-string">'name' =&gt; <span class="hljs-string">'John Doe']);</span></span></span></span></span></span></code></pre>
<p>您可以使用该<code>save</code>方法更新嵌入式模型(从2.0.0版开始提供):</p>
<pre><code class="hljs php">$author = $book-&gt;author;

$author-&gt;name = <span class="hljs-string">'Jane Doe';
$author-&gt;save();</span></code></pre>
<p>您可以使用以下新模型替换嵌入式模型:</p>
<pre><code class="hljs php">$newAuthor = <span class="hljs-keyword">new Author([<span class="hljs-string">'name' =&gt; <span class="hljs-string">'Jane Doe']);
$book-&gt;author()-&gt;save($newAuthor);</span></span></span></code></pre>
<h3 class="pos_23">与 MySQL 相关的信息</h3>
<p>如果你正在使用混合的MongoDB和SQL设置,那么你很幸运!模型将根据相关模型的类型自动返回MongoDB或SQL关系。当然,如果您希望此功能同时工作,那么您的SQL模型将需要使用该<code>Jenssegers\Mongodb\Eloquent\HybridRelations</code>特征。请注意,此功能仅适用于hasOne,hasMany和belongsTo关系。</p>
<p>示例基于SQL的用户模型:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">HybridRelations;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">User <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">use <span class="hljs-title">HybridRelations;

    <span class="hljs-keyword">protected $connection = <span class="hljs-string">'mysql';

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">messages<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;hasMany(<span class="hljs-string">'Message');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>而基于Mongodb的Message模型:</p>
<pre><code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">Jenssegers\<span class="hljs-title">Mongodb\<span class="hljs-title">Eloquent\<span class="hljs-title">Model <span class="hljs-title">as <span class="hljs-title">Eloquent;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Message <span class="hljs-keyword">extends <span class="hljs-title">Eloquent {

    <span class="hljs-keyword">protected $connection = <span class="hljs-string">'mongodb';

    <span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">user<span class="hljs-params">()
    {
      <span class="hljs-keyword">return <span class="hljs-keyword">$this-&gt;belongsTo(<span class="hljs-string">'User');
    }

}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<h3 class="pos_24">原始表达式</h3>
<p>这些表达式将直接注入查询中。</p>
<pre><code class="hljs php">User::whereRaw([<span class="hljs-string">'age' =&gt; <span class="hljs-keyword">array(<span class="hljs-string">'$gt' =&gt; <span class="hljs-number">30, <span class="hljs-string">'$lt' =&gt; <span class="hljs-number">40)])-&gt;get();</span></span></span></span></span></span></code></pre>
<p>您还可以在内部MongoCollection对象上执行原始表达式。如果在模型类上执行此操作,它将返回一组模型。如果在查询构建器上执行此操作,它将返回原始响应。</p>
<pre><code class="hljs php"><span class="hljs-comment">// Returns a collection of User models.
$models = User::raw(<span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($collection)
{
    <span class="hljs-keyword">return $collection-&gt;find();
});

<span class="hljs-comment">// Returns the original MongoCursor.
$cursor = DB::collection(<span class="hljs-string">'users')-&gt;raw(<span class="hljs-function"><span class="hljs-keyword">function<span class="hljs-params">($collection)
{
    <span class="hljs-keyword">return $collection-&gt;find();
});</span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>可选:如果未将闭包传递给raw方法,则可以访问内部MongoCollection对象:</p>
<pre><code class="hljs php">$model = User::raw()-&gt;findOne([<span class="hljs-string">'age' =&gt; <span class="hljs-keyword">array(<span class="hljs-string">'$lt' =&gt; <span class="hljs-number">18)]);</span></span></span></span></code></pre>
<p>可以像这样访问内部MongoClient和MongoDB对象:</p>
<pre><code class="hljs php">$client = DB::getMongoClient();
$db = DB::getMongoDB();</code></pre>
<h3 class="pos_25">MongoDB的具体操作</h3>
<p>游标超时</p>
<p>要防止MongoCursorTimeout异常,您可以手动设置将应用于游标的超时值:</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'users')-&gt;timeout(<span class="hljs-number">-1)-&gt;get();</span></span></code></pre>
<p>Upsert</p>
<p>更新或插入文档。update方法的其他选项将直接传递给本机更新方法。</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')
                     -&gt;update($data, [<span class="hljs-string">'upsert' =&gt; <span class="hljs-keyword">true]);</span></span></span></span></span></code></pre>
<p>Projections</p>
<p>您可以使用该<code>project</code>方法将投影应用于查询。</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'items')-&gt;project([<span class="hljs-string">'tags' =&gt; [<span class="hljs-string">'$slice' =&gt; <span class="hljs-number">1]])-&gt;get();
DB::collection(<span class="hljs-string">'items')-&gt;project([<span class="hljs-string">'tags' =&gt; [<span class="hljs-string">'$slice' =&gt; [<span class="hljs-number">3, <span class="hljs-number">7]]])-&gt;get();</span></span></span></span></span></span></span></span></span></code></pre>
<p>Projections with Pagination</p>
<pre><code class="hljs php">$limit = <span class="hljs-number">25;
$projections = [<span class="hljs-string">'id', <span class="hljs-string">'name'];
DB::collection(<span class="hljs-string">'items')-&gt;paginate($limit, $projections);</span></span></span></span></code></pre>
<p>Push</p>
<p>将单项添加到数组。</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;push(<span class="hljs-string">'items', <span class="hljs-string">'boots');
DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;push(<span class="hljs-string">'messages', [<span class="hljs-string">'from' =&gt; <span class="hljs-string">'Jane Doe', <span class="hljs-string">'message' =&gt; <span class="hljs-string">'Hi John']);</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>如果您不想要重复项,请将第三个参数设置为<code>true</code>:</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;push(<span class="hljs-string">'items', <span class="hljs-string">'boots', <span class="hljs-keyword">true);</span></span></span></span></span></span></code></pre>
<p>Pull</p>
<p>从数组中删除项目。</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;pull(<span class="hljs-string">'items', <span class="hljs-string">'boots');
DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;pull(<span class="hljs-string">'messages', [<span class="hljs-string">'from' =&gt; <span class="hljs-string">'Jane Doe', <span class="hljs-string">'message' =&gt; <span class="hljs-string">'Hi John']);</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>Unset</p>
<p>从文档中删除一个或多个字段。</p>
<pre><code class="hljs php">DB::collection(<span class="hljs-string">'users')-&gt;where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;unset(<span class="hljs-string">'note');</span></span></span></span></code></pre>
<p>您还可以在模型上执行取消设置。</p>
<pre><code class="hljs php">$user = User::where(<span class="hljs-string">'name', <span class="hljs-string">'John')-&gt;first();
$user-&gt;unset(<span class="hljs-string">'note');</span></span></span></code></pre>
<h3 class="pos_26">查询缓存 Query Caching</h3>
<p>您可以使用remember方法轻松缓存查询结果:</p>
<pre><code class="hljs php">$users = User::remember(<span class="hljs-number">10)-&gt;get();</span></code></pre>
<p><em>来自:http://laravel.com/docs/queries#caching-queries</em></p>
<h3 class="pos_27">查询日志 Query Logging</h3>
<p>默认情况下,Laravel会记录已为当前请求运行的所有查询的内存。但是,在某些情况下,例如插入大量行时,这可能会导致应用程序使用过多的内存。要禁用日志,您可以使用以下<code>disableQueryLog</code>方法:</p>
<pre><code class="hljs php">DB::connection()-&gt;disableQueryLog();</code></pre>
<p><em>来自:http://laravel.com/docs/database#query-logging</em></p>
</div>

</div>
<div id="MySignature" role="contentinfo">
    纵然世间炎凉百态!我自依旧初心不改!!<br><br>
来源:https://www.cnblogs.com/phpk/p/11377735.html
頁: [1]
查看完整版本: laravel操作mongo详细说明