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 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢