如何检查 laravel 5 和 mongodb 中相同 table 中具有相同值的两个字段
how to check two fields having same values in same table in laravel 5 with mongodb
我必须检查 laravel 5 中相同 table 中具有相同值的两个字段。我正在使用 Mongodb。
{
"id": "565d23ef5c2a4c9454355679",
"title": "Event1",
"summary": "test",
"total": NumberInt(87),
"remaining": NumberInt(87),
"status": "1"
}
我需要检查 "total" 和 "remaining" 字段是否相同。如何在 laravel 5.1 中编写查询。请帮忙。
您可以采用的一种方法是使用来自底层驱动程序提供的原始 MongoDB 集合对象的聚合框架方法。在 mongo shell 中,您基本上会 运行 以下聚合管道操作来比较两个字段和 return 满足该条件的文档:
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)"
}
]);
或使用$where
operator in the find()
查询:
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"));
我必须检查 laravel 5 中相同 table 中具有相同值的两个字段。我正在使用 Mongodb。
{
"id": "565d23ef5c2a4c9454355679",
"title": "Event1",
"summary": "test",
"total": NumberInt(87),
"remaining": NumberInt(87),
"status": "1"
}
我需要检查 "total" 和 "remaining" 字段是否相同。如何在 laravel 5.1 中编写查询。请帮忙。
您可以采用的一种方法是使用来自底层驱动程序提供的原始 MongoDB 集合对象的聚合框架方法。在 mongo shell 中,您基本上会 运行 以下聚合管道操作来比较两个字段和 return 满足该条件的文档:
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)"
}
]);
或使用$where
operator in the find()
查询:
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"));