c# driver 2.5.0, Array filters for update operations

1,637 views
Skip to first unread message

Itzi Kagan

unread,
Dec 27, 2017, 5:59:11 AM12/27/17
to mongodb-user
Is there any documentation or examples on how to do it with the c# driver 2.5.0

Thanks,
Itzhak

Wan Bachtiar

unread,
Jan 23, 2018, 9:10:17 PM1/23/18
to mongodb-user

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.

Itzi Kagan

unread,
Jan 23, 2018, 11:23:29 PM1/23/18
to mongodb-user
Thanks Wan,

In that example I have to write the field name ("grades") hard codded in the code.
I was hoping that the driver will do that as it does with all other operations.

Thanks,
Itzhak

Wan Bachtiar

unread,
Feb 26, 2018, 1:45:35 AM2/26/18
to mongodb-user

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 :

  • Example documents
  • Example code of what you’re trying to do
  • Expected outcome after the operation

Regards,
Wan.

Itzi Kagan

unread,
Feb 26, 2018, 3:31:14 PM2/26/18
to mongod...@googlegroups.com
Hi, Wan,

What I was hoping for, and still think it should be, is some sort of Builder<T> to to compose the expression, as there is for Filter, Projection, Update etc..
Having a variable means that I have to keep it somewhere in the code. I see it as not a desirable practice, to say the least.

In general, There is so little documentation, if at all, on the 2.5 version of the driver. We are not using any new feature yet, and would very much like to that.

Thanks,
Itzhak

--
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.

For more options, visit https://groups.google.com/d/optout.

jenny lurye

unread,
May 30, 2019, 7:54:36 AM5/30/19
to mongodb-user
Hi, Are there any updates on this topic?
I'm also looking for a strongly typed way to work with array filters instead of BsonDocument.
Perhaps there is another way (strongly typed as well) to update a property of an item that is deeply nested inside a sub-sub collection of the documents collection?
For example, if I have a collection of A's of the following structure:
{
     "_Id": "some guid"
      "Bs": [
          {
              "_id":"another guid",
              "Cs":[
                 {
                      "_id": "third id",
                      "property": "I'm the property that needs to be updated",
                 }   
               ]
          }
       ]
}

How can I update the property of C in a typed manner?
Thanks!
Jenny
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.

Wan Bachtiar

unread,
May 31, 2019, 12:59:01 AM5/31/19
to mongodb-user

Hi Jenny, 

Could you please open a new thread with the following information: 

* MongoDB server version 
* MongoDB .NET/C# driver version 
* Code snippet that you've tried 

Regards, 
Wan. 
Reply all
Reply to author
Forward
0 new messages