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
    }
}
原文地址:https://www.cnblogs.com/zihunqingxin/p/4447964.html