如何将多个嵌套的子文档字段的状态更新为真或假。

huangapple go评论59阅读模式
英文:

how to update the multiple nested sub document field status to true or false

问题

I need to update multiple nested sub-document fields with eid: '63b3f577763b3f5777' in the document to either true or false.

I've tried using the updateMany query, but it's not working. My schema is quite nested, and I don't have any other options.

Here's my document model:

{
  "_id": "63b3b63b3f57774024",
  "name": "dev",
  "email": "dev@gmail.com",
  "image": "https://sgp1.digitaloceanspaces.com",
  "tickets": [
    {
      "sid": "63b363b3f5777f5768",
      "eid": "63b3f577763b3f5777",
      "name": "john",
      "status": false,
      "_id": {
        "$oid": "63b463b3f5777178c4"
      }
    },
    {
      "sid": "63b363b3f5777f5769",
      "eid": "63b3f577763b3f5777",
      "name": "viswam",
      "status": false,
      "_id": {
        "$oid": "63b463b3f5777178c5"
      }
    },
    {
      "sid": "63b63b3f57773f5770",
      "eid": "63b3f577763b3f5777",
      "name": "dev",
      "status": false,
      "_id": {
        "$oid": "63b63b3f57774178c6"
      }
    },
    {
      "sid": "63b63b3f57773f5771",
      "eid": "63b3f577763b3f5777",
      "name": "kumar",
      "status": false,
      "_id": {
        "$oid": "63b463b3f5777178c7"
      }
    }
  ],
  "__v": 0
}

I need to update tickets.status to true or false. How can I check this field? Can anyone suggest the exact query to find and update this?

Specifications:

  • Node: v14.17.3
  • Mongoose: "^6.6.5"
  • MongoDB: Atlas
英文:

I need to update multiple nested sub document field which eid:63b3f577763b3f5777 in the document status to true or false,

I try updateMany query but its not working...

I know my schema is more nested but i don't have any other option

this is my document model

{
"_id":"63b3b63b3f57774024"
"name":"dev"
"email":"dev@gmail.com"
"image":"https://sgp1.digitaloceanspaces.com"

"tickets":[
{
"sid":"63b363b3f5777f5768"
"eid":"63b3f577763b3f5777"
"name":"john"
"status":false
_id:{
"$oid":"63b463b3f5777178c4"
}
},
{
"sid":"63b363b3f5777f5769"
"eid":"63b3f577763b3f5777"
"name":"viswam"
"status":false
"_id":{
"$oid":"63b463b3f5777178c5"
}
{
"sid":"63b63b3f57773f5770"
"eid":"63b3f577763b3f5777"
"name":"dev"
"status":false
"_id":{
"$oid":"63b63b3f57774178c6"
}
{
"sid":"63b63b3f57773f5771"
"eid":"63b3f577763b3f5777"
"name":"kumar"
"status":false
"_id":{
"$oid":"63b463b3f5777178c7"
}
""__v":0
}]

I need to update tickests.status to true or false.How to check this field?

Can anyone suggest me the exact query to find this?

Specification

  • node: v14.17.3,

  • mongoose: "^6.6.5",

  • mongodb:Atlas

答案1

得分: 0

尝试这段代码:

db.collections.updateMany(
  { "tickets": { "$elemMatch": { "eid": "63b3f577763b3f5777" } } },
  { "$set": { "tickets.$.status": false } }
)

如果您有任何查询,请告诉我。

英文:

Try this code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

db.collections.updateMany(
  { &quot;tickets&quot;: { &quot;$elemMatch&quot;: { &quot;eid&quot;: &quot;63b3f577763b3f5777&quot; } } }
  { &quot;$set&quot;: { &quot;tickets.$.status&quot;: false } }
)

<!-- end snippet -->

If you have any query. Please let me know

答案2

得分: 0

db.collections.updateMany
({"tickets.eid":"63b3f577763b3f5777"},
{"$set":{"tickets.$[elem].status":"false"}},
{"arrayFilters": [{"elem.eid":"63b3f577763b3f5777"}]})

英文:
db.collections.updateMany
({&quot;tickets.eid&quot;:&quot;63b3f577763b3f5777&quot;},
{&quot;$set&quot;:{&quot;tickets.$[elem].status&quot;:&quot;false&quot;}},
{&quot;arrayFilters&quot;: [{&quot;elem.eid&quot;:&quot;63b3f577763b3f5777&quot;}]
})

huangapple
  • 本文由 发表于 2023年1月4日 17:29:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75003444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定