将节点添加到exisitng文档mongodb

发布于 2025-02-12 17:05:37 字数 7462 浏览 0 评论 0原文

我试图用一个额外字段更新现有文档,因为

现有字段

[{
  "_id": {
    "$oid": "62a8ad644035e93c7ff24607"
  },
  "user_name": "[email protected]",
  "password": "1234",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "first_name": "ghgh",
    "middle_name": "",
    "last_name": "gh",
    "email": "[email protected]",
    "mobile_number": 1234567890
  },
  "address_details": {
    "country": {
      "country_id": {
        "$oid": "623012f9683f8fb7bd213c36"
      },
      "country_code": "IN"
    },
    "state": {
      "state_id": {
        "$oid": "623013d9683f8fb7bd2381c4"
      },
      "state_code": "MP"
    },
    "city": {
      "city_id": {
        "$oid": "6230139f683f8fb7bd21f1a9"
      },
      "city_name": "Akodia"
    },
    "address_line1": "",
    "address_line2": "",
    "pincode": ""
  },
  "is_verify": false,
  "last_accessed": {
    "$date": {
      "$numberLong": "1655221604144"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1655221604144"
    }
  },
  "course_details": {}
}] 

需要更新所有字段

[{
  "_id": {
    "$oid": "62bf2cf2a8c782741bbcc398"
  },
  "user_name": "[email protected]",
  "password": "$2b$12$NgLNYm5jUBmFUq.w15.qCO35GiTJX09jAnOpoGuPt9G7GNuOkVN7K",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "email": "[email protected]",
    "mobile_number": "9898989898",
    "first_name": "Fahim",
    "middle_name": "",
    "last_name": "Ashhab",
    "nationality": "Antiguans",
    "date_of_birth": "2003-02-04",
    "admission_year": "2022-23",
    "gender": "Male",
    "category": "General",
    "para_ability": {
      "is_disable": false,
      "name_of_disability": ""
    }
  },
  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  },
  "is_verify": true,
  "last_accessed": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "allocate_to_counselor": {
    "counselor_id": {
      "$oid": "62bfd13a5ce8a398ad101bd7"
    },
    "counselor_name": "test",
    "last_update": {
      "$date": {
        "$numberLong": "1656817948401"
      }
    }
  },
  "course_details": {
    "BSc": {
      "course_id": {
        "$oid": "628e03d94e22276b98231407"
      },
      "course_name": "BSc",
      "application_id": {
        "$oid": "62bf2cf2a8c782741bbcc399"
      },
      "status": "Incomplete",
      "specs": [
        {
          "spec_name": "Physician Assistant",
          "is_activated": true
        }
      ]
    }
  }
}]

查询不尝试任何事情发生< /strong>

db.collection.aggregate([
{$match:{"address_details.country": {$exists:true}}},
{$set:{address_details:{communication_address:"$address_details"}}}
])

第二次查询未尝试

db.studentsPrimaryDetails.updateMany({
    "address_details.communication_address": {"$exist":false}},
    {$set: {"address_details.communication_address":"$address_details"}})

所需的结果需要是 从这个

"address_details": {
    "country": {
      "country_id": {
        "$oid": "623012f9683f8fb7bd213c36"
      },
      "country_code": "IN"
    },
    "state": {
      "state_id": {
        "$oid": "623013d9683f8fb7bd2381c4"
      },
      "state_code": "MP"
    },
    "city": {
      "city_id": {
        "$oid": "6230139f683f8fb7bd21f1a9"
      },
      "city_name": "Akodia"
    },
    "address_line1": "",
    "address_line2": "",
    "pincode": ""
  }

到这个

  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  }

不知道我在做什么错。

编辑:

所需的完整输出

[{
  "_id": {
    "$oid": "62bf2cf2a8c782741bbcc398"
  },
  "user_name": "[email protected]",
  "password": "12345678",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "email": "[email protected]",
    "mobile_number": "9898989898",
    "first_name": "Fahim",
    "middle_name": "",
    "last_name": "Ashhab",
    "nationality": "Antiguans",
    "date_of_birth": "2003-02-04",
    "admission_year": "2022-23",
    "gender": "Male",
    "category": "General",
    "para_ability": {
      "is_disable": false,
      "name_of_disability": ""
    }
  },
  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  },
  "is_verify": true,
  "last_accessed": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "allocate_to_counselor": {
    "counselor_id": {
      "$oid": "62bfd13a5ce8a398ad101bd7"
    },
    "counselor_name": "viru chaudhary",
    "last_update": {
      "$date": {
        "$numberLong": "1656817948401"
      }
    }
  },
  "course_details": {
    "BSc": {
      "course_id": {
        "$oid": "628e03d94e22276b98231407"
      },
      "course_name": "BSc",
      "application_id": {
        "$oid": "62bf2cf2a8c782741bbcc399"
      },
      "status": "Incomplete",
      "specs": [
        {
          "spec_name": "Physician Assistant",
          "is_activated": true
        }
      ]
    }
  }
}]```

I am trying to update the existing document with one extra field as a new requirement in DB needs that

Existing field

[{
  "_id": {
    "$oid": "62a8ad644035e93c7ff24607"
  },
  "user_name": "[email protected]",
  "password": "1234",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "first_name": "ghgh",
    "middle_name": "",
    "last_name": "gh",
    "email": "[email protected]",
    "mobile_number": 1234567890
  },
  "address_details": {
    "country": {
      "country_id": {
        "$oid": "623012f9683f8fb7bd213c36"
      },
      "country_code": "IN"
    },
    "state": {
      "state_id": {
        "$oid": "623013d9683f8fb7bd2381c4"
      },
      "state_code": "MP"
    },
    "city": {
      "city_id": {
        "$oid": "6230139f683f8fb7bd21f1a9"
      },
      "city_name": "Akodia"
    },
    "address_line1": "",
    "address_line2": "",
    "pincode": ""
  },
  "is_verify": false,
  "last_accessed": {
    "$date": {
      "$numberLong": "1655221604144"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1655221604144"
    }
  },
  "course_details": {}
}] 

Needs to update all fields

[{
  "_id": {
    "$oid": "62bf2cf2a8c782741bbcc398"
  },
  "user_name": "[email protected]",
  "password": "$2b$12$NgLNYm5jUBmFUq.w15.qCO35GiTJX09jAnOpoGuPt9G7GNuOkVN7K",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "email": "[email protected]",
    "mobile_number": "9898989898",
    "first_name": "Fahim",
    "middle_name": "",
    "last_name": "Ashhab",
    "nationality": "Antiguans",
    "date_of_birth": "2003-02-04",
    "admission_year": "2022-23",
    "gender": "Male",
    "category": "General",
    "para_ability": {
      "is_disable": false,
      "name_of_disability": ""
    }
  },
  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  },
  "is_verify": true,
  "last_accessed": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "allocate_to_counselor": {
    "counselor_id": {
      "$oid": "62bfd13a5ce8a398ad101bd7"
    },
    "counselor_name": "test",
    "last_update": {
      "$date": {
        "$numberLong": "1656817948401"
      }
    }
  },
  "course_details": {
    "BSc": {
      "course_id": {
        "$oid": "628e03d94e22276b98231407"
      },
      "course_name": "BSc",
      "application_id": {
        "$oid": "62bf2cf2a8c782741bbcc399"
      },
      "status": "Incomplete",
      "specs": [
        {
          "spec_name": "Physician Assistant",
          "is_activated": true
        }
      ]
    }
  }
}]

Query Tried nothing is happening

db.collection.aggregate([
{$match:{"address_details.country": {$exists:true}}},
{$set:{address_details:{communication_address:"$address_details"}}}
])

2nd Query Tried didn't go through

db.studentsPrimaryDetails.updateMany({
    "address_details.communication_address": {"$exist":false}},
    {$set: {"address_details.communication_address":"$address_details"}})

The desired result needs to be
from this

"address_details": {
    "country": {
      "country_id": {
        "$oid": "623012f9683f8fb7bd213c36"
      },
      "country_code": "IN"
    },
    "state": {
      "state_id": {
        "$oid": "623013d9683f8fb7bd2381c4"
      },
      "state_code": "MP"
    },
    "city": {
      "city_id": {
        "$oid": "6230139f683f8fb7bd21f1a9"
      },
      "city_name": "Akodia"
    },
    "address_line1": "",
    "address_line2": "",
    "pincode": ""
  }

to this

  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  }

Don't know what I am doing wrong.

Edit :

Desired full output

[{
  "_id": {
    "$oid": "62bf2cf2a8c782741bbcc398"
  },
  "user_name": "[email protected]",
  "password": "12345678",
  "college_id": {
    "$oid": "628dfd41ef796e8f757a5c13"
  },
  "basic_details": {
    "email": "[email protected]",
    "mobile_number": "9898989898",
    "first_name": "Fahim",
    "middle_name": "",
    "last_name": "Ashhab",
    "nationality": "Antiguans",
    "date_of_birth": "2003-02-04",
    "admission_year": "2022-23",
    "gender": "Male",
    "category": "General",
    "para_ability": {
      "is_disable": false,
      "name_of_disability": ""
    }
  },
  "address_details": {
    "communication_address": {
      "country": {
        "country_id": {
          "$oid": "623012f9683f8fb7bd213c36"
        },
        "country_code": "IN"
      },
      "state": {
        "state_id": {
          "$oid": "623013d9683f8fb7bd2380ef"
        },
        "state_code": "AP"
      },
      "city": {
        "city_id": {
          "$oid": "6230139f683f8fb7bd21ebc8"
        },
        "city_name": "Akasahebpet"
      },
      "address_line1": "jjkjk",
      "address_line2": "uiui",
      "pincode": "989898"
    }
  },
  "is_verify": true,
  "last_accessed": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "created_at": {
    "$date": {
      "$numberLong": "1656696050670"
    }
  },
  "allocate_to_counselor": {
    "counselor_id": {
      "$oid": "62bfd13a5ce8a398ad101bd7"
    },
    "counselor_name": "viru chaudhary",
    "last_update": {
      "$date": {
        "$numberLong": "1656817948401"
      }
    }
  },
  "course_details": {
    "BSc": {
      "course_id": {
        "$oid": "628e03d94e22276b98231407"
      },
      "course_name": "BSc",
      "application_id": {
        "$oid": "62bf2cf2a8c782741bbcc399"
      },
      "status": "Incomplete",
      "specs": [
        {
          "spec_name": "Physician Assistant",
          "is_activated": true
        }
      ]
    }
  }
}]```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2025-02-19 17:05:39

一个选项是:

  1. 使用$已存在而不是$存在
  2. 使用管道,以便能够$ set从另一个字段的值中的值
db.collection.updateMany(
  {"address_details.communication_address": {$exists: false}},
  [{$set: {address_detailsB: {communication_address: "$address_details"}}},
   {$set: {
     address_details: "$address_detailsB",
     address_detailsB: "$REMOVE"}
   }
])

请参阅它如何在 Playground示例示例

汇总查询您首次尝试的,不会更新DB,它仅返回数据。为了将其用作更新,您需要添加$ MERGE步骤。

One option is:

  1. Use $exists instead of $exist
  2. Use pipeline in order to be able to $set the value from another fields' value
db.collection.updateMany(
  {"address_details.communication_address": {$exists: false}},
  [{$set: {address_detailsB: {communication_address: "$address_details"}}},
   {$set: {
     address_details: "$address_detailsB",
     address_detailsB: "$REMOVE"}
   }
])

See how it works on the playground example

The aggregation query that you first tried, does not not update the db, it only returns data. In order to use it as an updater you need to add a $merge step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文