Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何对对象内的对象进行计数(无计数变量索引)_Mongodb - Fatal编程技术网

Mongodb 如何对对象内的对象进行计数(无计数变量索引)

Mongodb 如何对对象内的对象进行计数(无计数变量索引),mongodb,Mongodb,我在mongodb中存储了以下模型: { "_id": (MongoId) "user": "X", "field-name-1":{ "obj-1": 12345, "obj-2": 54321, "obj-3": 67890, (...) }, "field-name-2:{ "obj-1": {"foo":"bar", "bar":"foo"}, "obj

我在mongodb中存储了以下模型:

{
    "_id": (MongoId)
    "user": "X",
    "field-name-1":{
        "obj-1": 12345,
        "obj-2": 54321,
        "obj-3": 67890,
        (...)
    },
    "field-name-2:{
        "obj-1": {"foo":"bar", "bar":"foo"},
        "obj-2": {"foo":"bar2", "bar":"foo2"},
        (...)
    }
}
如何找到“field-name-1”中包含5个以上对象的ocurrence?“field-name-2”也一样吗

我不能使用变量来计算对象的数量,因为我经常在“field-name-1”/“field-name-2”中插入值

谢谢


已尝试(未成功):


$where在这种情况下应为合格:

function findMyDocument(key, value) {
    var f = this[key];
    if (typeof f != "object" || typeof value != "number") {
        return false;
    }
    var count = 0;
    for (var x in f) {
        if (++count > value) {
            return true;
        }
    }
    return false;
}

// store above function to database for invoking
db.system.js.insert({_id:"findMyDocument", value: findMyDocument});

// apply here
db.summoners.find({$where: "findMyDocument.call(this, 'field-name-1', 5);"});
db.summoners.find({$where: "findMyDocument.call(this, 'field-name-2', 5);"});

似乎这是一个伪装成对象的数组。但即使是数组也不支持对其
$size
进行范围查询。所以,如果不提取对象,恐怕你做不了什么。那么固定大小呢?我计划每天至少运行两次,这样它就不会超过给定的大小。您可以查询特定
$size
的数组,是的。@SergioTulentsev但对于数组,您可以使用聚合框架。对于对象,映射/减少可能是唯一的方法。不过速度很慢。
function findMyDocument(key, value) {
    var f = this[key];
    if (typeof f != "object" || typeof value != "number") {
        return false;
    }
    var count = 0;
    for (var x in f) {
        if (++count > value) {
            return true;
        }
    }
    return false;
}

// store above function to database for invoking
db.system.js.insert({_id:"findMyDocument", value: findMyDocument});

// apply here
db.summoners.find({$where: "findMyDocument.call(this, 'field-name-1', 5);"});
db.summoners.find({$where: "findMyDocument.call(this, 'field-name-2', 5);"});