- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有一个这样的集合:
{
_id : 544e97123c9ef694fc68e21b,
title: "First Title",
notebook: {
title: "Misc",
slug: "misc"
}
}
{
_id: 54ab035a849788d0921d8eb2,
title: "Second Title",
notebook: {
title: "Personal",
slug: "personal"
}
}
{
_id: 544e97123c9ef694fc68e21b,
title: "Third Title",
notebook: {
title: "Misc",
slug: "misc"
}
}
在我看来,我希望能够显示笔记本标题已被使用的次数以及指向该特定笔记本的 slug 的链接(不分特定顺序)。例如:
<a href="/notebooks/misc">Misc(2)</a>
<a href="/notebooks/personal">Personal(1)</a>
我通过浏览每个文档来实现这一点,但问题是它有重复,因为它正在浏览每个文档。所以在我看来,它看起来像这样:
<a href="/notebooks/misc">Misc(2)</a>
<a href="/notebooks/personal">Personal(1)</a>
<a href="/notebooks/misc">Misc(2)</a>
如何抓取notebook.title、notebook.slug,对它们进行计数,并且没有重复项?
这是我目前的做法(这会导致重复):
function countNotebooks(notes) {
var table = Object.create(null);
for (var i = 0; i < notes.length; i++) {
if (typeof table[notes[i].notebook.slug] === 'undefined') {
table[notes[i].notebook.slug] = 1;
} else {
table[notes[i].notebook.slug] += 1;
}
}
return table;
}
app.get('/notebooks', function(req, res) {
Note.find(function(err, notes) {
if (err) {
throw err;
}
res.render('notebooks/index.html', {
title: 'All Notebooks',
jumbotron: 'Notebooks',
notes: notes,
notesTable: countNotebooks(notes)
});
});
});
笔记本/index.html:
{% for note in notes %}
<article class="note">
<h3 class="note-title">
<a href="/notebooks/{{ note.notebook.slug }}">{{ note.notebook.title }}</a> <span class="count">({{ notesTable[note.notebook.slug] }})</span>
</h3>
</article>
{% endfor %}
最佳答案
您可以通过根据公用 key 对数据进行分组来实现此目的。 aggregation framework MongoDB 就是为这种聚合和操作而设计的。
首先,我可以更正您的数据样本吗,因为您有重复的 _id
值,这是不允许的。
{
"_id" : ObjectId("544e97123c9ef694fc68e21b"),
"title" : "First Title",
"notebook" : {
"title" : "Misc",
"slug" : "misc"
}
},
{
"_id" : ObjectId("54ab035a849788d0921d8eb2"),
"title" : "Second Title",
"notebook" : {
"title" : "Personal",
"slug" : "personal"
}
},
{
"_id" : ObjectId("54ac074fa8a621d3fd49ac91"),
"title" : "Third Title",
"notebook" : {
"title" : "Misc",
"slug" : "misc"
}
}
要为数据中出现的“slug”添加“计数”值,您可以形成如下管道:
Note.aggregate([
// Group on the slug values and put other fields in an array
{ "$group": {
"_id": "$notebook.slug",
"count": { "$sum": 1 },
"docs": {
"$push": {
"_id": "$_id",
"title": "$title",
"notebook": "$notebook"
}
}
}},
// Unwind the created array elements
{ "$unwind": "$docs" },
// Re-structure back to original form
{ "$project": {
"_id": "$docs._id",
"title": "$docs.title",
"count": "$count",
"notebook": "$docs.notebook"
}},
// Sort in original order (or as desired)
{ "$sort": { "_id": 1 } }
],function(err,result) {
});
这会给你带来这样的结果:
{
"_id" : ObjectId("544e97123c9ef694fc68e21b"),
"count" : 2,
"title" : "First Title",
"notebook" : {
"title" : "Misc",
"slug" : "misc"
}
},
{
"_id" : ObjectId("54ab035a849788d0921d8eb2"),
"count" : 1,
"title" : "Second Title",
"notebook" : {
"title" : "Personal",
"slug" : "personal"
}
},
{
"_id" : ObjectId("54ac074fa8a621d3fd49ac91"),
"count" : 2,
"title" : "Third Title",
"notebook" : {
"title" : "Misc",
"slug" : "misc"
}
}
如果您想“保留文档”,但如果您只想要像“面数”这样的独特“slugs”,那么只需使用第一个 $group
与 $first
笔记本标题上的 而不是 $push
与其他内容:
Note.aggregate([
// Group on the slug values and put other fields in an array
{ "$group": {
"_id": "$notebook.slug",
"count": { "$sum": 1 },
"title": { "$first": "$notebook.title" }
}},
],function(err,result) {
});
它应该是相当不言自明的,但只是为了总结。初始 $group
使用“slug”的值来计算 $sum
的出现次数。运算符(operator)。为了保留文档数据的其余部分,将其放置在“slug”下的数组中,使用 $push
.
分组后,使用 $unwind
将数组非规范化为文档,然后使用 $project
简单地重新构造回原始形式。决赛 $sort
操作提供原始顺序或您想要的任何内容,因为顺序在 $group
期间发生了更改。管道阶段。
这不仅可以获得结果,而且还允许您使用 $limit
对数据进行“分页”和 $skip
具有适当计数值的运算符,甚至根据需要根据这些计数值对数据进行排序。
查看完整的ggregation pipeline operator reference有关完整说明和其他可以在此处完成的操作。
关于node.js - 计算某个字段在集合中出现的次数,同时保留所有数据不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806604/
仅使用POSIX shell 的特性,是否有一个“简单命令”,它什么也不做,也不改变$? 的值。人们通常描述:作为 shell 的无操作命令,但这总是设置 $?为零,所以这不是我想要的。 这是生成 s
我需要保留当前的 GlBlendFunc 以便在我做一些工作后可以恢复它。似乎这不是可以用 GLPushAttrib 保存的属性之一,还有其他类似的方法可以用来保存状态吗? 最佳答案 glGet与
我目前正致力于创建 Fuseki 三重存储浏览器的项目。我需要可视化 TripleStore 中的所有数据并使应用程序可浏览。唯一的问题是 QuerySolution 遗漏了三元组中的“”。 如果我使
我知道没有必要保留委托(delegate),以避免保留循环。我在一次采访中碰巧遇到了一个问题,“如果保留了 appDelegate 会怎样?”。我对此没有答案,并根据我的知识在这里寻求答案。谢谢 最佳
我正在用 C 编写第一个更严肃的程序,但我陷入了困境。我需要将此列表排序为不同的单独文件,因此它看起来像这样: BE30B Berlin 2014-04-02 Gale 02 BE30B Berlin
我有 4 个页面,我使用 ajax 和 historyPopState 在它们之间进行更改。但是有一个问题,因为页面非常不同,它们使用不同的样式表和脚本。我可以一遍又一遍地下载它们,但我想问一下是否有
我有一个表单,其中包含从一个表创建的多行(与其他表没有关系)。当我保存表单时,我所做的每项更改都会保存,但数据库中确实有一个额外的空行。请参阅下文(希望)了解所有必要的信息。 PropertyAddi
我正在编写一个可以在 Canvas 上绘制气泡的应用程序。我有 MainActivity,它的布局是一个简单的 LinearLayout,我将其用作 fragment 的容器。当我在 Canvas 上
我想弄清楚为什么我通过这段代码得到 EXC_BAD_ACESS。我没有线索。谁能帮帮我。 - (void)loadJsonFile:(NSString*)fileName { NSError
我正在编写一个代码,它将遍历单词中的每个单词,在字典中查找它们,然后将字典值附加到计数器。但是,如果我打印计数器,我只会从我的 if 语句中获取最后一个数字(如果有的话)。如果我将 print cou
抱歉,标题有点困惑。 我想隐藏这些选择选项,只在用户选择按钮时显示。一旦用户选择了一个按钮,我希望选择字段将继续显示......但是如果用户选择了不同的按钮,我想从以前的选择中重置所选的选项并炫耀。
>>> t = "first%s\n" >>> t = t %("second") >>> print t firstsecond 无论如何我可以保留最后的“\n”并得到“firstsecond\n”
我试图弄清楚如何更改 if 语句中的变量并使其保持全局不变。 用户输入:!change Hi var A = "Hello" if (msg.content.includes ('!change'))
您好,我有一个关于在重新创建 Activity 时保留 fragment 的问题。我听说一种方法是在 onCreate 方法中使用 setRetainFragment(true) 。问题是——这与跟踪
我想知道是否有人可以帮助我解决下面代码中的内存管理问题。我对 rootController 特别感兴趣,它是在我执行 initWithRootViewController 时被保留还是在窗口 addS
我想确保我在这里正确理解了内存管理。是否有任何特殊原因在这里使用其中一种 assignCurrentDate 方法而不是另一种方法?此外,所有这些都不会导致内存泄漏,对吗? 在 .h 中我们有: NS
我对 Angular2 删除尾部斜杠有疑问。我已经设置了我的 dotnet 核心应用程序来添加它们,但是一旦加载了 js,它们就会被删除。 在 Angular2 中甚至可能吗? (我的客户需要它,所以
local_settings.py 反模式的原因之一是把 SECRET_KEY, AWS设置文件中的键等值有问题: secret 通常应该是这样的: secret !将它们保存在版本控制中意味着拥有存
import('./A'); import('./B'); import('./C'); export class Person {}; A、B 和 C 是纯 JS (es5) 库,它们使用全局 wi
df 是一个测试数据框,其中我只想保留 Hits 列中包含 | 字符的行和行其中包含 hits(即 Hit1、Hit2 等)以及 Hits 列中的空白单元格。 df 从开始 (^) 到结束 ($)
我是一名优秀的程序员,十分优秀!