[CSHARP-1046] Type-safe $set with positional operator "$" Created: 21/Aug/14  Updated: 09/Mar/15  Resolved: 09/Mar/15

Status: Closed
Project: C# Driver
Component/s: API
Affects Version/s: 1.9.2
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Svetoslav Milenov Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-531 Support the $ positional operator whe... Closed
Backwards Compatibility: Fully Compatible

 Description   

Currently the only way to use a positional operator & for updating a field in embedded array is to use the not type safe Update.Set with manually constructing the statement like:

Update.Set("MyArray.$.Value", newVal);

A method can be added to the generic Update<TDocument> and UpdateBuilder<TDocument> to avoid hard-codding strings (there is already similar approach with Query<T>.ElemMatch):

Update<MyDocument>.SetPositional(d => d.MyArray, a => a.Value, newVal);



 Comments   
Comment by Craig Wilson [ 09/Mar/15 ]

This has been resolved. See the commit link in CSHARP-531.

It is only applicable to the new API in 2.0.0. -1 can be used as an indexer or with GetElementAt to indicate to us to use the positional operator. For example:

Builders<Entity>.Update.Set(x => x.MyArray[-1].Value, 10);
// will yield {$set: { "MyArray.$.Value", 10 } }

Comment by Svetoslav Milenov [ 21/Aug/14 ]

Pull request submitted: https://github.com/mongodb/mongo-csharp-driver/pull/187

Generated at Wed Feb 07 21:38:31 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.