您可以使用 Model::where() 等方法或 DB 门面中的方法匹配文档,然后调用 get() 方法检索结果,从而从集合中检索多个文档。
Pass a query filter to the where() method to retrieve documents that meet a set of criteria. When you call the get() method, MongoDB returns the matching documents according to their natural order in the collection or according to the sort order that you can specify by using the orderBy() method.
提示
要学习;了解使用 Laravel 集成检索文档的其他方法,请参阅读取操作指南。
例子
从以下 Eloquent 和 Query Builder 标签页中选择,查看使用每个相应查询语法的同一操作的用法示例:
此示例将执行以下动作:
使用
MovieEloquent 模型表示sample_mflix数据库中的movies集合从
movies集合中检索并打印与查询筛选器匹配的文档
该示例在Movie模型上调用以下方法:
where():匹配runtime字段的值大于900的文档orderBy():按_id值升序对匹配的文档进行排序get():以 Laravel集合对象的形式检索查询结果
$movies = Movie::where('runtime', '>', 900) ->orderBy('_id') ->get();
此示例将执行以下动作:
通过从
DB门面调用table()方法来访问movies集合从
movies集合中检索并打印与查询筛选器匹配的文档
该示例调用以下查询构建器方法:
where():匹配runtime字段的值大于900的文档orderBy():按_id值升序对匹配的文档进行排序get():以 Laravel集合对象的形式检索查询结果
$movies = DB::table('movies') ->where('runtime', '>', 900) ->orderBy('_id') ->get();
要了解如何编辑 Laravel 应用程序以运行用法示例,请参阅用法示例登录页面。