dragon188199 2015-07-02 11:55
浏览 31
已采纳

使用PHP在Mongo Embedded Document中搜索

I have a MongoDB collection as given below:

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"
        }
    ]
}];

I want to search on "reason" in "code" field in above document with following operators : 1. Equals - return all code in which atleast one matches with "reason" value 2. Not Equal - return all code in which no one matches the value with "reason" field This is how I have done it in javascript :

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

But I am unable to do this in PHP using array. I have also tried "$elemMatch" but that does not work with "Not Equal". I cannot use command() in my case and I need it to be done using arrays.

  • 写回答

1条回答 默认 最新

  • douxi3977 2015-07-02 12:14
    关注

    I didn't tested output but You should do something like following

    1) Without using $elemMatch

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

    2) using $elemMatch

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

    Edit You can use aggregation like -

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

    and its equivalent php code is (not tested with php mongo)

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

    In addition, if you have at least PHP5.4, you can use simpler array syntax. Transformation to PHP is then trivial, you simply replace curly braces with square brackets and colons with arrows

     db.collection.aggregate(["$unwind":"$code"],["$match":["code.reason":["$ne":"Blacklist Account Email"]]])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?