patternMinor
Delete document from nested array in MongoDB
Viewed 0 times
deletearraymongodbnesteddocumentfrom
Problem
I've created a collection with following insert statement
```
db.test.insert({
"_id": 1,
"updatedAt": ISODate("2016-05-27T05:37:44.928Z"),
"created_at": ISODate("2016-05-13T09:41:23.000Z"),
"batchId": "BATCH-Con-0",
"size": 7,
"currentlySubscribed": 0,
"startDate": ISODate("2016-05-15T18:30:00.000Z"),
"endDate": ISODate("2016-07-08T18:30:00.000Z"),
"dailyHours": 6,
"trainer": null,
"status": "Started",
"attendance": [
{
"date": ISODate("2016-05-04T18:30:00.000Z"),
"students": [
{
"studentId": 1,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": 2,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": ("5735a3266c1e1dc423c302cf"),
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
}
]
},
{
"date": ISODate("2016-05-05T18:30:00.000Z"),
"students": [
{
"studentId": 1,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": 2,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": 3,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
```
db.test.insert({
"_id": 1,
"updatedAt": ISODate("2016-05-27T05:37:44.928Z"),
"created_at": ISODate("2016-05-13T09:41:23.000Z"),
"batchId": "BATCH-Con-0",
"size": 7,
"currentlySubscribed": 0,
"startDate": ISODate("2016-05-15T18:30:00.000Z"),
"endDate": ISODate("2016-07-08T18:30:00.000Z"),
"dailyHours": 6,
"trainer": null,
"status": "Started",
"attendance": [
{
"date": ISODate("2016-05-04T18:30:00.000Z"),
"students": [
{
"studentId": 1,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": 2,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": ("5735a3266c1e1dc423c302cf"),
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
}
]
},
{
"date": ISODate("2016-05-05T18:30:00.000Z"),
"students": [
{
"studentId": 1,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": 2,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
"attendanceStatus": "Present"
},
{
"studentId": 3,
"entryTime": ISODate("1969-12-31T19:31:00.000Z"),
"exitTime": ISODate("1969-12-31T19:31:00.000Z"),
Solution
If you want to use the $ positional operator, you have to have matched the array items already, something like this:
Remember that this will only update the first matching item in the attendance array in each document, so you will have to run this update more than once to make it update each attendance item.
db.test.update(
{"attendance.students.studentId":{$in:[1,2,3]}},
{$pull: {"attendance.$.students": {studentId: {$in:[1,2,3]}}}},
{multi:true}
)Remember that this will only update the first matching item in the attendance array in each document, so you will have to run this update more than once to make it update each attendance item.
Code Snippets
db.test.update(
{"attendance.students.studentId":{$in:[1,2,3]}},
{$pull: {"attendance.$.students": {studentId: {$in:[1,2,3]}}}},
{multi:true}
)Context
StackExchange Database Administrators Q#139717, answer score: 3
Revisions (0)
No revisions yet.