【问题标题】:how to check two fields having same values in same table in laravel 5 with mongodb如何使用mongodb检查laravel 5中同一张表中具有相同值的两个字段
【发布时间】:2015-10-06 23:31:54
【问题描述】:

我必须检查 laravel 5 中同一张表中具有相同值的两个字段。我使用的是 Mongodb。

{
"id": "565d23ef5c2a4c9454355679",
"title": "Event1",
"summary": "test",
"total": NumberInt(87),
"remaining": NumberInt(87),
"status": "1"
}

我需要检查“总计”和“剩余”字段是否相同。如何在 laravel 5.1 中编写查询。请帮忙。

【问题讨论】:

    标签: mongodb laravel-5.1


    【解决方案1】:

    您可以采用的一种方法是使用底层驱动程序提供的原始 MongoDB 集合对象中的聚合框架方法。在 mongo shell 中,您实际上将运行以下聚合管道操作来比较两个字段并返回满足该条件的文档:

    db.collection.aggregate([
        {
            "$project": {
                "isMatch": { "$eq" : ["$total", "$remaining"]  }, // similar to "valueof(total) == valueof(remaining)"
                "id" : 1,
                "title" : 1,
                "summary" : 1,
                "total" : 1,
                "remaining" : 1,
                "status" : 1
            }
        }, 
        {
            "$match": { "isMatch": true  } // filter to get documents that only satisfy "valueof(total) == valueof(remaining)"      
        }
    ]);
    

    或者在 find() 查询中使用 $where 运算符:

    db.collection.find({ "$where" : "this.total == this.remaining" })
    

    因此在 laravel 中,您可以使用 raw expressions 获取文档,如下所示

    $result = DB::collection("collectionName") -> raw(function ($collection)
    {
        return $collection->aggregate(array(
            array(
                "$project" => array(
                    "id" => 1,
                    "title" => 1,
                    "summary" => 1,
                    "total" => 1,
                    "remaining" => 1,
                    "status" => 1,
                    "isMatch" => array(
                        "$eq" => array( "$total", "$remaining" )
                    )                
                )
            ),
            array(
                "$match" => array(
                    "isMatch" => true               
                )
            ) 
        ));
    });
    

    对于$where,您可以将表达式直接注入到查询中:

    Model::whereRaw(array("$where" => "this.total == this.remaining"))->get();
    

    或者在查询构建器上执行的内部 MongoCollection 对象上使用原始表达式。请注意,使用 raw() 方法需要使用游标,因为它是低级调用:

    $result = Model::raw()->find(array("$where" => "this.total == this.remaining"));
    

    【讨论】:

    • $result = Event::whereRaw(array("where" => array('this.tickets', '=', 'this.remaining')))->get();我使用了这个查询,但我在 laravel 中得到了空结果。输出:集合 {#335 ▼ #items: [] }
    • @Nimisha 你得到一个空的结果,因为查询是错误的;而不是$result = Event::whereRaw(array("where" => array('this.tickets', '=', 'this.remaining')))->get();,您应该按照答案中的结构写$result = Event::whereRaw(array("$where" => "this.tickets == this.remaining"))->get();,密切注意数组元素。
    • 当我使用这个 $result = Event::whereRaw(array("$where" => "this.tickets == this.remaining"))->get();我得到“未定义的变量:哪里”错误。
    • 我试过查询 $result= Event::raw()->find(array('$where' => array('this.tickets == this.remaining')));但它显示的结果如下: MongoCursor {#325 ▼ ns: "eventsflip.events" limit: 0 batchSize: 0 skip: 0 flags: 0 query: array:1 [▼ "$where" => array:1 [▼ 0 => "this.tickets == this.remaining" ] ] fields: {#331} started_iterating: false dead: false } 如何获取数据?
    • $result = Event::whereRaw(array('$where' => "this.tickets == this.remaining"))->get();得到了工作。非常感谢。
    猜你喜欢
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 2022-12-05
    • 2013-12-23
    • 1970-01-01
    相关资源
    最近更新 更多