laravel mongodb日期与mongodb iso日期

$users = User::whereBetween(
             'created_at', array(
                 Carbon::createFromDate(2015, 10, 1),
                 Carbon::createFromDate(2015, 10, 31)
             )
         )->get();
<?php
 
use Jenssegers\Mongodb\Model as Eloquent;
 
class User extends Eloquent {
 
    use SoftDeletingTrait;
 
    /**
     * Collection for Model
     *
     * @var String
     */
    protected $collection = "users";
 
    /**
     * Connection for model
     *
     * @var type
     */
    protected $connection = 'mongodb';
 
    protected $dates = array('created_at');
}
$users = User::where('created_at', '>', new DateTime('-1 day'))->get();
$start = new MongoDate(strtotime("2015-10-01 00:00:00"));
$stop = new MongoDate(strtotime("2015-10-31 00:00:00"));
 
$users = DB::collection('users')->whereBetween('created_at', array($start, $stop))->get();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB 中,可以使用聚合管道来进行多表查询。聚合管道是一种操作数据的框架,可以在多个阶段对数据进行转换和处理。下面是一个使用 MongoDB 聚合管道进行多表查询的示例: 假设我们有两个集合,一个是用户集合(users),一个是订单集合(orders)。每个订单都对应一个用户。我们想要查询所有订单,并将其对应的用户信息也一并返回。 1. 首先,在 orders 集合中添加一个字段 userId,表示该订单对应的用户 ID。 2. 使用聚合管道进行查询,具体步骤如下: a. 使用 $lookup 阶段连接 users 集合和 orders 集合,将 orders 中的 userId 与 users 中的 _id 进行匹配。$lookup 阶段会将匹配的用户信息添加到每个订单文档中。 b. 使用 $unwind 阶段展开 orders 集合中的数组字段(如果有的话)。 c. 使用 $project 阶段选择要返回的字段,可以同时选择 orders 和 users 集合中的字段。 d. 使用 $match 阶段筛选符合条件的订单,可以根据订单状态、时间等条件进行筛选。 下面是一个示例代码: ``` db.orders.aggregate([ { $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "user" } }, { $unwind: "$items" }, { $project: { _id: 1, userId: 1, status: 1, total: { $sum: "$items.price" }, user: { $arrayElemAt: [ "$user", 0 ] } } }, { $match: { status: "completed" } } ]) ``` 这个查询会返回所有状态为 completed 的订单,每个订单文档中都包含一个 user 子文档,其中包含该订单对应的用户信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值