我应该使用mongoDB中的哪个聚合函数?



我有一个集合如下:

{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962700",
"Result": "NotEnrolled",
"enrollDate": "4/21/2021",
"Name": "THOMAS Edison",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
} 
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962700",
"Result": "NotEnrolled",
"enrollDate": "5/21/2021",
"Name": "THOMAS Edison",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
}
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962700",
"Result": "NotEnrolled",
"enrollDate": "5/21/2021",
"Name": "THOMAS Edison",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
}
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962701",
"Result": "Enrolled",
"enrollDate": "4/21/2021",
"Name": "Jim Miller",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
} 
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962701",
"Result": "Enrolled",
"enrollDate": "5/21/2021",
"Name": "Jim Miller",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
}
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962701",
"Result": "Enrolled",
"enrollDate": "5/21/2021",
"Name": "Jim Miller",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
}

现在错误地将具有相同日期('5/21/2021')的相同empID的20条记录插入到DB中。我想在数据库中只保留该日期和雇员的1条记录,并删除其余19条记录。这意味着对于empID,我只想保留"enrollDate":"4/21/2021"的记录和"enrollDate":"5/21/2021"的一个记录,并删除"enrollDate":"5/21/2021"的重复记录。相同"empId":"101962701".

我如何在Mongodb中形成删除查询?

预期输出

{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962700",
"Result": "NotEnrolled",
"enrollDate": "4/21/2021",
"Name": "THOMAS Edison",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
} 
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962700",
"Result": "NotEnrolled",
"enrollDate": "5/21/2021",
"Name": "THOMAS Edison",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
}
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962701",
"Result": "Enrolled",
"enrollDate": "4/21/2021",
"Name": "Jim Miller",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
} 
{
"_id": {
"$oid": "6100b7c226aa5c7c0bb665e1"
},
"empId": "101962701",
"Result": "Enrolled",
"enrollDate": "5/21/2021",
"Name": "Jim Miller",
"Flag": "NEGATIVE",
"createdDateTime": {
"$date": "2021-06-30T06:00:00.000Z"
}
}

我想用enrollDate删除重复的empId

应该是这个:

db.collection.aggregate([
{
$group: {
_id: { empId: "$empId", enrollDate: "$enrollDate" },
data: { $first: "$$ROOT" }
}
},
{ $replaceRoot: { newRoot: "$data" } }
])

Mongo操场

相关内容

  • 没有找到相关文章

最新更新