i have a list schema and a question set schema. the quetsionSet schema is embedded inside the list schema. its working fine but how can i update anything inside the array of embedded document i.e. here i want to change the listname of all the documents inside questionSet (array of questionSet documents).
here is an example of my list document model
JavaScript
x
21
21
1
{ "_id" : ObjectId("60f2cc07275bbb30d8cb268e"),
2
"listName" : "dsa",
3
"aboutList" : "dsa queestions",
4
questionSet" : [ { "solved" : false,
5
"_id" : ObjectId("60f2cc12275bbb30d8cb2695"),
6
"topic" : "array",
7
"name" : "array is best",
8
"url" : "www.arr.com",
9
"listname" : "dsa",
10
"__v" : 0 },
11
{ "solved" : false,
12
"_id" : ObjectId("60f2cc1b275bbb30d8cb269d"),
13
"topic" : "linked list",
14
"name" : "reverse list",
15
"url" : "www.list.com",
16
"listname" : "dsa",
17
"__v" : 0 }
18
],
19
"__v" : 2
20
}
21
Advertisement
Answer
you can use the following in your case
JavaScript
1
9
1
db.<collection_name>.updateOne(
2
{ "_id" : ObjectId("60f2cc07275bbb30d8cb268e")},
3
{
4
$set: {
5
'questionSet.$[].listname': "javascript"
6
}
7
}
8
)
9