英文:
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(
{ "tickets": { "$elemMatch": { "eid": "63b3f577763b3f5777" } } }
{ "$set": { "tickets.$.status": 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
({"tickets.eid":"63b3f577763b3f5777"},
{"$set":{"tickets.$[elem].status":"false"}},
{"arrayFilters": [{"elem.eid":"63b3f577763b3f5777"}]
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论