Hi Team,
I am getting different formats of change streams for updateOne
query on List element using Array filters for single and multiple matches.
- When a single element is matched and modified, we get the expected difference in the form a dot separate paths along with the values before and after change.
- When multiple elements are matched and modified, we get the entire list before and after the change.
Please find below example with the query and generated change stream:
Single Match:
db.getCollection("test").find({_id:1},{"list":1})[
{
_id: 1,
list: [
{ key1: 'a', position: 1 },
{ key1: 'b', position: 2 }
]
}
]
Query:
db.getCollection("test").updateMany(
{_id:1},
{"$set":{"list.$[element].position":3}},
{"arrayFilters":[{"element.key1":"a"}]})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Updated Object:
db.getCollection("test").find({_id:1},{list:1})[
{
_id: 1,
list: [
{ key1: 'a', position: 3 },
{ key1: 'b', position: 2 }
]
}
]`
`
Change Stream generated:
[
{
operationType: 'update',
clusterTime: Timestamp({ t: 1723030373, i: 20 }),
wallTime: '2024-08-07T11:32:53.484Z',
ns: { db: 'testDB', coll: 'test' },
documentKey: { _id: '1' },
updateDescription: {
updatedFields: { 'list.2.position': 1 },
removedFields: [],
truncatedArrays: [],
updatedFieldsBeforeChange: { 'list.2.position': 3 }
}
}
]
Multiple Match:
db.getCollection("test").find({_id:1},{"list":1})[
{
_id: 1,
list: [
{ key1: 'a', position: 1 },
{ key1: 'b', position: 2 },
{ key1: 'a', position: 3 }
]
}
]
Query:
db.getCollection("test").updateMany(
{_id:1},
{"$set":{"list.$[element].position":4}},
{"arrayFilters":[{"element.key1":"a"}]})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Updated Object:
db.getCollection("test").find({_id:1},{list:1})[
{
_id: 1,
list: [
{ key1: 'a', position: 4 },
{ key1: 'b', position: 2 },
{ key1: 'a', position: 4 }
]
}
]
Change Stream generated:
[
{
_id: ObjectId("66b35945e6d6bb7b49b705f3"),
operationType: 'update',
clusterTime: Timestamp({ t: 1723029829, i: 8 }),
wallTime: '2024-08-07T11:23:49.134Z',
ns: { db: 'testDB', coll: 'test' },
documentKey: { _id: 1 },
updateDescription: {
updatedFields: {
list: [
{ key1: 'a', position: 1 },
{ key1: 'b', position: 2 },
{ key1: 'a', position: 3 }
]
},
removedFields: [],
truncatedArrays: [],
updatedFieldsBeforeChange: {
list: [
{ key1: 'a', position: 4 },
{ key1: 'b', position: 2 },
{ key1: 'a', position: 4 }
]
}
}
}
]