Hi Itzhak,
Using the examples provided in MongoDB manual v3.6+ Specify arrayFilters for an Array Update Operations:
db.students.insert([
{ "_id" : 1, "grades" : [ 95, 92, 90 ] },
{ "_id" : 2, "grades" : [ 98, 100, 102 ] },
{ "_id" : 3, "grades" : [ 95, 110, 100 ] }
]);
To perform the same operation as below:
db.runCommand({
update: "students",
updates: [
{ q: { grades: { $gte: 100 } }, u: { $set: { "grades.$[element]" : 100 } }, arrayFilters: [ { "element": { $gte: 100 } } ], multi: true}
]
})
Using MongoDB .Net/C# Driver v2.5 the equivalent code example would be:
var collection = database.GetCollection<BsonDocument>("students");
var filter = Builders<BsonDocument>.Filter.Gte("grades", 100);
var update = Builders<BsonDocument>.Update.Set("grades.$[element]", 100);
var arrayFilters = new List<ArrayFilterDefinition>();
ArrayFilterDefinition<BsonDocument> arrayFilter = new BsonDocument("element", new BsonDocument("$gte", 100));
arrayFilters.Add(arrayFilter);
var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
var result = collection.UpdateMany(filter, update, updateOptions);
Regards,
Wan.
In that example I have to write the field name (“grades”) hard codded in the code.
Hi Itzhak,
You can substitute the string grades with a variable.
Are you referring to an unknown field ? Generally you should know which field you’re trying to update.
If not, then I would suggest to reconsider your application data model. See also Data Model Examples and Patterns
I was hoping that the driver will do that as it does with all other operations.
Could you elaborate more with :
Regards,
Wan.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/LdRNIrt6jlM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/993e7f17-177e-41f1-9b88-b00d3cb1acdd%40googlegroups.com.
To unsubscribe from this group and all its topics, send an email to mongod...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.