假设我们有以下数据
*家长*
{ _id: 10, child: ObjectID(20) }
{ _id: 11, child: ObjectID(21) }
{ _id: 12, child: ObjectID(23) }
*儿童*
{ _id: 20, name: 'test-1' }
{ _id: 21, name: 'test-2' }
{ _id: 22, name: 'test-3' }
{ _id: 23, name: 'test-1' }
我想知道用什么猫鼬来获得以下结果。
{ _id: 'test-1', count: 2}
{ _id: 'test-2', count: 1}
我目前正在使用以下查询,但结果中的_id始终为{ _id: null, count: 3 }
:(
Parent.aggregate([{
$group: {
_id: '$child.name',
count: { $sum: 1 }
}
}])
感谢任何形式的帮助。
答案 0 :(得分:1)
创建一个单独的集合以避免对两个不同集合进行两次查询是更好的方法。
首先,您应该从父集合中找出所有不同的子ID,并将其分配给它如下所示:
<sp:groupedForDateTime each="{paginatedAppointments}"
as="appointmentsByDay"
groupBy="startDate"
format="d.m.Y"
groupKey="startDate">
Termine am {startDate->f:format.date(format:'d.m.Y')}
<f:groupedFor each="{appointmentsByDay}"
as="appointmentsByLawyer"
groupBy="lawyer.fullName"
groupKey="lawyerName">
<f:link.action class="lcapp-delete-link" action="deleteMultiple" arguments="{appointments: appointmentsByLawyer}">
Termine löschen
</f:link.action>
</f:groupedFor>
</sp:groupedForDateTime>
//How do I get information into the div after clicking on a "lcapp-delete-link"?
<div style="display:hidden">
<div id="lcapp-dialog-delete-msg" class="lcapp-dialog-delete-msg">
<f:form action="deleteMultiple" method="post" id="messageForm">
//<h2>Termine für !!(Lawyer clicked: ){lawyer.fullName}!! am !!(Corresponding date: ){startDate->f:format.date(format:'d.m.Y')}!! löschen</h2>
<p>Begründung (optional)</p>
<f:form.textarea id="msg" name="msg"/>
</f:form>
</div>
</div>
<script>
$(document).ready(function (r) {
$('.lcapp-delete-link').click(function (ev) {
ev.preventDefault();
ev.stopPropagation();
var goto = this.href;
$('.lcapp-dialog-delete-msg').dialog({
resizable: false,
height: 500,
width: 430,
modal: true,
buttons: {
'Löschen und Benachrichtigen': function () {
$('#messageForm').attr('action', goto).submit();
},
'Abbrechen': function () {
$(this).dialog("close");
$(this).dialog("destroy");
}
}
});
});
});
</script>
之后在var allChildIds = db.parent.distinct("child")
集合中使用了这些子ID,如下所示:
children