如何从对象内部的多个数组中找到数组内部的OneAndUpdate MongoDB对象

ecbunoof  于 2023-01-08  发布在  Go
关注(0)|答案(1)|浏览(103)

我是mongodD的新手。虽然我已经写了正确的逻辑来推对象到cardsData bankCardsArray里面,但是我不能做编辑卡片的逻辑。

我在MongoDB中有以下数据,

UserData = {
    "_id":  "63b43ab32fc8d3c100cafecc",
    "name": "Praveen Lohar",
    "email": "pr********@gmail.com",
    "password": "$2b$12$3nwifHakrBu94BwLXAC4Nu16Kw0.xyW8vAIPTMSgY7cYttVklDIZq",
    "loginIdsArray": [
      
        {
        "title": "Airtel App",
        "logoIndex": 3,
        "category": "Finance",
        "app": "Airtel Thanks",
        "username": "p******@gmail.com ",
        "password": "xxxxxxx",
        "isFavourite": false,
        "_id":"63b440ee738a50fc333df3ca"
      }
    ],

    "cardsData": {
      "bankCardsArray": [
        {
          "title": "SBI CC",
          "logoIndex": 72,
          "category": "Bank",
          "cardHolder": "Prsscscs",
          "cardNumber": "524xxxxxxxx20xx",
          "expiry": "23/54",
          "cvv": "00",
          "isFavourite": false,
          "_id": "63b974adb9acaa24a4ebec8b"
           
        },  
        {
            "title": "AXIS CC",
            "logoIndex": 85,
            "category": "Bank",
            "cardHolder": "blsllslss",
            "cardNumber": "524xxxxxxxx20xx",
            "expiry": "23/54",
            "cvv": "00",
            "isFavourite": false,
            "_id": "63b974adb9acaa24a4ebe5f"
             
          }
      ],
      "identityCardsArray": [
        {
          "title": "Aadhar Card",
          "logoIndex": 98,
          "category": "Identity",
          "cardHolder": "Name_1",
          "cardNumber": "54*******455454",
          "issueDate": "2012",
          "dob": "23/05",
          "isFavourite": false,
          "_id":"63b974e6bd0d03c116955f67"
        }
      ],
      "licenseCardsArray": [
        {
          "title": "Two Wheeler License",
          "logoIndex": 0,
          "category": "License",
          "cardHolder": "Klaus Michealson",
          "licenseNumber": "RJ/22/ADL-542/474/01",
          "expiry": "2036",
          "dob": "13/06",
          "isFavourite": false,
          "_id":"63b97535bd0d03c116955f69"
          
        }
      ]
    }
  }

1.我想查找具有_id的用户
1.然后,对于该用户,我希望获得cardsData对象
1.然后在cardsData对象中,我想得到bankCardsArray
1.然后在bankCardsArray中,我想更新具有特定id的卡的字段

Const res = findOneAndUpdate(
{ 'cardsData.cardArrays._id':card_id },
{ $set{ 'cardsData.cardsArray.$.title': req.body.title });
dly7yett

dly7yett1#

使用.“”操作符访问嵌套字段。请参考这些mongodb文档query-array-of-documentsquery-embedded-documents。希望这能有所帮助。

相关问题