多标签嵌套数组中的MongoDB查找
我在Mongodb很新。我正在尝试在多标签嵌套数组中进行查找。我的数据看起来像波纹管。
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
}
]
}
}
]
我试图像波纹管一样跑步。
{
"$lookup": {
"from": "shifts",
"localField": "shifts.shift.shiftId",
"foreignField": "_id",
"as": "shifts.shift.shiftId"
}
}
我得到结果。
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
]
}
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
]
}
}
}
]
但是我的期望数据看起来像是波纹管。
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": {
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": {
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
}
]
}
}
]
这是
shifts.shifts.shift
缺少的字段。 ShiftID
正在替换Shift Array下的所有字段。请帮助我。 mongoplayground
I am very new in MongoDB. I am trying to make lookup in multi label nested array. My data is looks like bellow.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
}
]
}
}
]
And I am trying to run lookup like bellow.
{
"$lookup": {
"from": "shifts",
"localField": "shifts.shift.shiftId",
"foreignField": "_id",
"as": "shifts.shift.shiftId"
}
}
I am getting the result.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
]
}
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
]
}
}
}
]
But my expectation data should looks like bellow.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": {
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": {
"_id": "622bb0f4b88dc92e3c2cac56",
"name": "Day"
}
}
]
}
}
]
Here is date
field under shifts.shift
is missing. shiftId
is replacing all the field under shift array. Please help me out.
mongoplayground
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用查找结果覆盖阵列的原始内容。
考虑使用子管道将日期存储为变量,并将其分配给查找结果。
这是 mongo playground 供您参考。
You are overriding the original content of the array with your lookup result.
Consider using subpipeline to store the date as variable and assign it to the lookup result.
Here is the Mongo playground for your reference.
抱歉,我犯了一个错误来表明我的要求。我已经通过多个班次改变了比赛地面。 修改的游戏地面。结果看起来像是波纹管。
Sorry I made a mistake to show my requirement. I have changed the play ground with multiple shift. Modified play ground . And it result should looks like bellow.