mongodb use where and custom function to query mongodb存储过程

function name regexObjSubObjKey

function code 

function(proto,value) {
    var match=false; 
    var reg = new RegExp('.*'+value);
        print(JSON.stringify(this.obj));
    print(this['arr_'+proto]);
    for(var key in this.obj['arr_'+proto]){
          print(key);
        if(key!='count'&&key!='isgame'){
             match = reg.test(key);
            if(match){
                return true;
            }
        }
    }
    return false;
}

query demo

精简

db.test.find({$where:"regexObjSubObjKey('qq','1234567')"})

标准

db.test.find({$and:[{arr_qq:{$exists:true}},{$where:"regexObjSubObjKey('qq','1234567')"}]})

 

数据内容

{
    "_id": "7654321",
    "arr_qq": {
        "1234567": {
            "count": 8
        },
        "abcdefg": {
            "count": 8
        },
        "count": 16
    }
}
posted @ 2015-04-22 16:49  cclient  阅读(301)  评论(0编辑  收藏  举报