- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在使用 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)的容器
但现在我只想在 bar 比 foo 旧(使用时间字段)时计算 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/
对于相当简单的表结构,即。人员、标准和 PersonCriteria(组合表),我现在设置了一个查询,选择所有符合所有选定条件的人。 此时查询本身看起来像这样: SELECT p.PersonID
我在使用高级 SQL 查询时遇到了一些问题,而且我已经有很长时间没有使用 SQL 数据库了。我们使用 MySQL。 背景: 我们将使用两个表: “交易表” 表:expire_history +----
我找不到错误。也许你可以帮助我:我的代码如下: var data = {"product":[{"config":[{"id":"1","price":"100","sku":"1054879634
我有一个列表列表的列表(最后一个列表并不重要) data = [[[['f', 0], 'C'], [['X', 0], 'X']], [[['s', 1], 'X'], [['X', 0], 'X'
我想准备将使用表格的 session ,并在另一个网站上将新项目添加到 session 中。 默认.cs string[] tab = new string[100];
我知道有一些像: Bubble sort Insertion sort Shell sort Merge sort Heapsort Quicksort Bucket sort Radix sort
像https://softwareengineering.stackexchange.com/questions/150616/return-random-list-item-by-its-weigh
我正在开发一个 posix 脚本 (Linux),它获取一个网页,将内容存储在一个变量中并查找字符串“SUCCESS”。如果找到字符串,则不执行循环内容,如果没有找到字符串,则反复执行循环,直到找到为
我不确定这个问题是否已在其他地方得到解答,而且我似乎无法通过谷歌找到任何不是“Hello World”示例的内容...我正在使用 C# .NET 4.0 进行编码。 我正在尝试开发一个控制台应用程序,
我创建了一个房地产网站,我希望按照列表的最后更新和完整性对列表进行排序。所以我一直想弄清楚如何结合最近更新的列表按mysql中的字段(completion_score)进行排序。完成分数将采用 1
只所以称为“高级”用法,是因为我连switch的最基础的用法都还没有掌握,so,接下来讲的其实还是它的基础用法! switch 语句和具有同样表达式的一系列的 IF 语句相似。很多场合下需要把同一
之前的章节中,我们学习了 XML DOM,并使用了 XML DOM 的 getElementsByTagName() 方法从 XML 文档中取回数据 本章节我们将继续学习其它重要的 XML DOM
我对我尝试编写的 SQL 查询有疑问。 我需要从数据库中查询数据。该数据库除其他外,还包括以下 3 个字段: Account_ID #, Date_Created, Time_Created 我需要编
我正在使用非常激进的视频压缩,例如 -crf 51 .我将其用于“艺术”效果,因此从普通视频压缩的角度来看,我所做的可能没有意义。 到目前为止,我只使用了非常基本的压缩控制,只使用了 -crf。或 -
我真的在学习 lucene 和 ravendb 上的绳索 - 我在 Raven 中有以下文档 - { "InternalEvent": { "Desec": "MachineInfo: 1
通常 grep 命令用于显示包含指定模式的行。有没有办法在包含指定模式的行之前和之后显示 n 行? 这可以使用awk来实现吗? 最佳答案 是的,使用 grep -B num1 -A num2 在匹配之
我搜索了高低,并尝试了几个小时来操纵似乎适合的各种其他查询,但我没有快乐。 我试图加入 Microsoft SQL Server 2005 中的几个表,其中一个示例是: Company Table (
我有一个如下所示的 XML 文件: teacher1Name
我将如何在 CF 中创建此语句? 显然括号不起作用,但说明了我想要完成的工作。这是什么语法? 编辑: 好的,我了解如何使用 EQ 等等。我有点匆忙地发布了这个。我的问题是关于括号。以这种方式使用它们
主要问题:我需要使用具体对象结构对任何对象结构进行类型扩展。 我在 VS Code 中测试的默认值。 我的解决方案: /** @template A @typedef {{[Ki in keyof A
我是一名优秀的程序员,十分优秀!