Hi, I currently working with java and mongodb.
original documents :
{
_id: 'test1',
arr: [ '111', '222', '333', '333' ]
},
{
_id: 'test2',
arr: [ '111', '222', '333' ]
}
executed java code No.1 :
Bson filter = Filters.and(Filters.exists("arr"), Filters.all("arr", "333"));
Bson bson = Updates.set("arr.$", "999");
UpdateResult result = collection.updateMany(filter, bson);
executed java code No.2 :
Bson filter = Filters.all("arr", "333");
Bson bson = Updates.set("arr.$", "999");
UpdateResult result = collection.updateMany(filter, bson);
Result of No.1 :
{
_id: 'test1',
arr: [ '999', '222', '333', '333' ]
},
{
_id: 'test2',
arr: [ '999', '222', '333' ]
}
Result of No.2 :
{
_id: 'test1',
arr: [ '111', '222', '999', '333' ]
},
{
_id: 'test2',
arr: [ '111', '222', '999' ]
}
What I intended was No.2, and by deleting “and” and “exists”, it worked the way I wanted.
but at the same time, new questions arose.
Isn’t a filter that checks if a field exists a “redundant” condition?
so I thought the result would be the same even if I deleted it.
I don’t know why these two filters give different results…