使用PHP在Mongo Embedded Document中搜索

时间:2015-07-02 11:55:39

标签: php arrays mongodb

我有一个MongoDB集合,如下所示:

var myCollection = [{
    "_id" : ObjectId("559158f6473f6c540f000282"),
    "code" : [ 
        {
            "score" : -100,
            "rule_name" : "Account Email",
            "header" : "Blacklist",
            "reason" : "Blacklist Account Email"
        }
    ]
},
{
    "_id" : ObjectId("5591592c473f6c540f000284"),
    "code" : [ 
        {
            "score" : -100,
            "rule_name" : "Account Email",
            "header" : "Blacklist",
            "reason" : "Blacklist Account Email"
        }
    ]
},{
    "_id" : ObjectId("55915931473f6c540f000286"),
    "code" : [ 
        {
            "score" : -100,
            "rule_name" : "Account Email",
            "header" : "Blacklist",
            "reason" : "Blacklist Account Email"
        }
    ]
},{
    "_id" : ObjectId("55915996473f6cd40b00010e"),
    "code" : [ 
        {
            "score" : 23,
            "rule_name" : "Transaction Check",
            "header" : "",
            "reason" : " Transaction Check"
        }, 
        {
            "score" : -100,
            "rule_name" : "Account Email",
            "header" : "Blacklist",
            "reason" : "Blacklist Account Email"
        }
    ]
},{
    "_id" : ObjectId("55915a3d473f6c540f000288"),
    "code" : [ 
        {
            "score" : 23,
            "rule_name" : "Transaction Check",
            "header" : "",
            "reason" : " Transaction Check"
        }, 
        {
            "score" : -100,
            "rule_name" : "Account Email",
            "header" : "Blacklist",
            "reason" : "Blacklist Account Email"
        }
    ]
},{
    "_id" : ObjectId("55942f1f473f6c7808000037"),
    "code" : [ 
        {
            "score" : 80,
            "rule_name" : "Test Rule",
            "header" : "",
            "reason" : "Test Rule"
        }
    ]
},{
    "_id" : ObjectId("55943328473f6cfc1000002a"),
    "code" : [ 
        {
            "score" : 80,
            "rule_name" : "Test Rule",
            "header" : "",
            "reason" : "Test Rule"
        }
    ]
},{
    "_id" : ObjectId("559433f9473f6cf012000032"),
    "code" : [ 
        {
            "score" : 80,
            "rule_name" : "Test Rule",
            "header" : "",
            "reason" : "Test Rule"
        }
    ]
}];

我想使用以下运算符在上述文档的“代码”字段中搜索“reason”: 1.等于 - 返回至少与“reason”值匹配的所有代码 2.不等于 - 返回所有没有人将该值与“reason”字段匹配的代码 这是我在javascript中完成它的方式:

db.my_collection.find({
    "code.reason" : {"$ne" : "Blacklist Account Email"}
})

但我无法在使用数组的PHP中执行此操作。我也试过“$ elemMatch”,但这不适用于“不等于”。我不能在我的情况下使用command(),我需要使用数组来完成。

1 个答案:

答案 0 :(得分:0)

我没有测试输出,但您应该执行以下操作

1)不使用$elemMatch

$criteria = array('code.reason' => array("$ne" =>"Blacklist Account Email")));$collection->find($criteria);

2)使用$elemMatch

$criteria = array(
    "code"=>array("$elemMatch"=>array(
    "reason"=> array("$ne"=>"Blacklist Account Email"
    )))
);

$collection->find($criteria);

修改 您可以使用聚合 -

db.collection.aggregate({$unwind:"$code"},{$match:{"code.reason":{$ne:"Blacklist Account Email"}}})

及其等效的PHP代码是(未使用php mongo 进行测试)

$criteria = array(array("$unwind" => "$code"), array("$match" => array("code.reason" => array("$ne" => "Blacklist Account Email"))));
$collection->aggregate($criteria);

此外,如果您至少拥有PHP5.4,则可以使用更简单的数组语法。转换到PHP是微不足道的,你只需用方括号替换花括号,用箭头替换冒号

 db.collection.aggregate(["$unwind":"$code"],["$match":["code.reason":["$ne":"Blacklist Account Email"]]])