嵌套数组中的MongoDB查找
我正在尝试在MongoDB嵌套阵列中进行查找。我的数据看起来像。
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
},
{
"date": "2022-05-02",
"shiftId": "622b55f8f59dcdd1ab9b36b1"
},
]
}
},
{
"_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": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
},
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"date": "2022-05-02",
"shiftId": [
{
"_id": "622b55f8f59dcdd1ab9b36b1",
"date": "2022-05-02",
"name": "Morning"
}
]
}
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
}
]
但是我的要求数据看起来像是波纹管。 shiftID
应嵌套在移位数组中,以及偏移数据。
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
},
{
"date": "2022-05-02",
"shiftId": [
{
"_id": "622b55f8f59dcdd1ab9b36b1",
"date": "2022-05-02",
"name": "Morning"
}
]
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
}
]
这是丢失的日期字段。并且还需要对轮班阵列进行分组。请帮助我。 Playground
I am trying to make lookup in MongoDB nested array. My Data is looks like.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
},
{
"date": "2022-05-02",
"shiftId": "622b55f8f59dcdd1ab9b36b1"
},
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": "622bb0f4b88dc92e3c2cac56"
}
]
}
}
]
I was trying with the lookup.
{
"$lookup": {
"from": "shifts",
"localField": "shifts.shift.shiftId",
"foreignField": "_id",
"as": "shifts.shift.shiftId"
}
}
And getting the result.
[
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
},
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": {
"date": "2022-05-02",
"shiftId": [
{
"_id": "622b55f8f59dcdd1ab9b36b1",
"date": "2022-05-02",
"name": "Morning"
}
]
}
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
}
]
But my require data should looks like as bellow. shiftId
should nested under shift array along with shifts data.
{
"_id": "621eedae92979fd8f0e9451d",
"name": "Pallab Koley",
"shifts": {
"_id": "62636b9fcbda6d2b17f5cae0",
"month": "2022-05",
"shift": [
{
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
},
{
"date": "2022-05-02",
"shiftId": [
{
"_id": "622b55f8f59dcdd1ab9b36b1",
"date": "2022-05-02",
"name": "Morning"
}
]
}
]
}
},
{
"_id": "62626a7446ba9a911a623b37",
"name": "Pinki Das",
"shifts": {
"_id": "62636ba4cbda6d2b17f5cae1",
"month": "2022-05",
"shift": {
"date": "2022-05-01",
"shiftId": [
{
"_id": "622bb0f4b88dc92e3c2cac56",
"date": "2022-05-01",
"name": "Day"
}
]
}
}
}
]
Here is date field under shift is missing. And also need to group the shift array. Please help me out. PlayGround
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ set
$ lookup
Use
$set
after$lookup
mongoplayground