gpt4 book ai didi

MongoDB 高级计数查询,取决于文档的内容

转载 作者:可可西里 更新时间:2023-11-01 10:44:34 25 4
gpt4 key购买 nike

我在使用 MongoDB 进行高级计数查询时遇到问题。

我正在尝试对文档字段内部 对象进行计数,但前提是存在与查询匹配的前一个元素并且较旧 数组中。

让我解释一下....

我有一个容器文档,看起来像:

{
"_id" : ...,
[...]
"objects" : [ ]
}

对象字段内部:
我有一个对象文档,看起来像:

[
{
"_id" : ...,
"name" : "foo",
"properties" = [ ],
"time" = 000000042
}

{
"_id" : ...,
"name" : "bar",
"properties" = [ ],
"time" = 000000424
}

{
"_id" : ...,
"name" : "baz",
"properties" = [ ],
"time" = 000004242
}

现在我计算有多少容器文档包含:
包含对象 1 (foo) 的容器的 count
count 包含对象 1 和 2(foo 和 bar)的容器,
count 包含对象 1、2 和 3(foo、bar、baz)的容器

但现在我只想在 barfoo (使用时间字段)时计算 foo 和 bar...< br/>包含对象 1 (foo) 的容器的 count
count 包含对象 1 和 2(foo 和 bar)的容器,AND foo.time < bar.time
count 包含对象 1、2 和 3(foo、bar、baz)的容器 AND foo.time < bar.time < baz.time

问题是时间字段需要为每个容器更改。换句话说:我如何对每个文档使用动态查询

这里是一个代码示例:

foreach ($COUNTER[ARRAY_FIELDS_NAME_TO_COUNT] as $key => $value)
{
// Build the query (Name & properties)
$match[$key] = array('$elemMatch' => array('name' => $value['name']));
foreach ($value['properties'] as $propertyName => $propertyValue)
$match[$key]['$elemMatch']["properties.$propertyName"] = $propertyValue;

// Time checking
if ($key > 0)
{
//FIXME with a... dynamics query searching inside current doc??
// or a special var set to the previous object matched... or MapReduce..
}


// Make the query
$query = array('objects' => array('$all' => $match));

$result[$key]['count'] = $db->person->count($query);
}

我是 MongoDB 的新手,我真的不知道什么是有效地做到这一点的最佳实践!

问候!

最佳答案

以下是如何在 shell (javascript) 中使用聚合框架来执行此操作。

请注意,为了便于阅读,我将其分成多行,实际查询是最后一行。我用过 foo bar 和 baz,但当然你想调整到更合适的东西。

match = { "$match" : { "objects.name" : "foo" } };
unwind = { "$unwind" : "$objects" };
projectDates = {"$project" : {
"_id" : 1,
"objects" : 1,
"fooDate" : {
"$cond" : [
{
"$eq" : [
"$objects.name",
"foo"
]
},
"$objects.time",
0
]
},
"barDate" : {
"$cond" : [
{
"$eq" : [
"$objects.name",
"bar"
]
},
"$objects.time",
0
]
},
"bazDate" : {
"$cond" : [
{
"$eq" : [
"$objects.name",
"baz"
]
},
"$objects.time",
0
]
}
}
};
group = {"$group" : {
"_id" : "$_id",
"objects" : {
"$push" : "$objects"
},
"maxFooDate" : {
"$max" : "$fooDate"
},
"maxBarDate" : {
"$max" : "$barDate"
},
"maxBazDate" : {
"$max" : "$bazDate"
}
}
};
projectCount = {"$project" : {
"_id" : 1,
"foos" : {
"$add" : [
1,
0
]
},
"foosAndBars" : {
"$cond" : [
{
"$lt" : [
"$maxFooDate",
"$maxBarDate"
]
},
1,
0
]
},
"foosBarsAndBazs" : {
"$cond" : [
{
"$and" : [
{
"$lt" : [
"$maxBarDate",
"$maxBazDate"
]
},
{
"$lt" : [
"$maxFooDate",
"$maxBarDate"
]
}
]
},
1,
0
]
}
}
};
countFoos = {"$group" : {
"_id" : "FoosBarsBazs",
"Foos" : {
"$sum" : "$foos"
},
"FBs" : {
"$sum" : "$foosAndBars"
},
"FBBs" : {
"$sum" : "$foosBarsAndBazs"
}
}
};


db.collection.aggregate([match, unwind, projectDates, projectCount, countFoos]).result

关于MongoDB 高级计数查询,取决于文档的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12749900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com