MongoDB $indexOfArray不适用于数组中的嵌套对象

ldioqlga  于 2023-04-05  发布在  Go
关注(0)|答案(1)|浏览(146)

我有一个文档,其中包含以下数据

{
    "_id": "640311ab0469a9c4eaf3d2bd",
    "id": 4051,
    "email": "manoj123@gmail.com",
    "password": "SomeNew SecurePassword",
    "about": null,
    "token": "7f471974-ae46-4ac0-a882-1980c300c4d6",
    "country": "India",
    "location": null,
    "lng": 0,
    "lat": 0,
    "dob": null,
    "gender": 0,
    "userType": 1,
    "userStatus": 1,
    "profilePicture": "Images/9b291404-bc2e-4806-88c5-08d29e65a5ad.png",
    "coverPicture": "Images/44af97d9-b8c9-4ec1-a099-010671db25b7.png",
    "enablefollowme": false,
    "sendmenotifications": false,
    "sendTextmessages": false,
    "enabletagging": false,
    "createdAt": "2020-01-01T11:13:27.1107739",
    "updatedAt": "2020-01-02T09:16:49.284864",
    "livelng": 77.389849,
    "livelat": 28.6282231,
    "liveLocation": "Unnamed Road, Chhijarsi, Sector 63, Noida, Uttar Pradesh 201307, India",
    "creditBalance": 130,
    "myCash": 0,
    "data": {
        "name": "ThisIsAwesmoe",
        "arr": [
            1,
            100,
            3,
            4,
            5,
            6,
            7,
            8,
            9
        ],
        "hobies": {
            "composer": [
                "anirudh",
                {
                    "co_singer": [
                        "rakshitha",
                        "divagar"
                    ]
                },
                "yuvan",
                "rahman"
            ],
            "music": "helo"
        }
    },
    "scores": [
        {
            "subject": "math",
            "score": 100
        },
        {
            "subject": "physics",
            "score": 85
        },
        {
            "subject": "chemistry",
            "score": 95
        }
    ],
    "fix": 1,
    "hello": 1,
    "recent_views": [
        200
    ],
    "exam": "",
    "subject": "",
    "arr": {
        "name": "sibidharan",
        "pass": "hello",
        "score": {
            "subject": {
                "minor": "zoology",
                "major": "biology",
                "others": [
                    "evs",
                    {
                        "name": "shiro",
                        "inarr": [
                            200,
                            2,
                            3,
                            {
                                "sub": "testsub",
                                "newsu": "aksjdad",
                                "secret": "skdjfnsdkfjnsdfsdf"
                            },
                            4,
                            12
                        ]
                    }
                ]
            },
            "score": 40,
            "new": "not7",
            "hello": {
                "arr": [
                    5,
                    2
                ]
            }
        }
    },
    "name": "Manoj Kumar",
    "d": [
        1,
        3,
        4,
        5
    ],
    "score": {},
    "hgf": 5
}

我正在尝试查找位于此路径data.hobies.composer.1.co_singer中的$indexOfArray,并使用以下查询:

[
  {
    "$match": {
      "id": 4051
    }
  },
  {
    "$project": {
      "index": {
        "$indexOfArray": [
          "$data.hobies.composer.1.co_singer",
          "divagar"
        ]
      }
    }
  }
]

这段python代码使用pymongo返回-1

result = list(self._collection.aggregate(pipeline))
return result[0]["index"]

pipeline是上面的查询。
如果嵌套路径中没有数组,则它可以工作,但如果有数组,则$indexOfArray返回-1。
我错过了什么?

bf1o4zei

bf1o4zei1#

首先使用composer数组上的“$ElemAt”访问co-singer数组,然后使用'$indexofArray'查找索引

db.collection.aggregate([
      {
        "$match": {
          "id": 4051
        }
      },
      {
        "$addFields": {
          "co_singer": {
            $arrayElemAt: [
              "$data.hobies.composer",
              1
            ]
          }
        }
      },
      {
        "$project": {
          "index": {
            "$indexOfArray": [
              "$co_singer.co_singer",
              "divagar"
            ]
          }
        }
      }
    ])

检查此管道,这将正确返回索引Demo @Mongo playground

相关问题