C# driver. How do we build update expression like { $pull : { field : {$gt: 3} } }?

747 views
Skip to first unread message

Roman Kuzmin

unread,
Aug 3, 2011, 5:13:54 AM8/3/11
to mongodb-user
Example at http://www.mongodb.org/display/DOCS/Updating:
{ $pull : { field : {$gt: 3} } } removes array elements greater than 3

This way (in PowerShell but it's not much different from C#):
$query = [MongoDB.Driver.Builders.Query]::GT('field', 3)
$update = [MongoDB.Driver.Builders.Update]::Pull('field', $query)

.. gives a different expression:
{ "$pull" : { "field" : { "field" : { "$gt" : 3 } } } }

How do we build the example expression { $pull : { field : {$gt:
3} } } using C# driver builders?

Robert Stam

unread,
Aug 3, 2011, 10:44:51 AM8/3/11
to mongodb-user
That particular variation of $pull was missed when designing the
Update builder. I've create a JIRA ticket for this:

https://jira.mongodb.org/browse/CSHARP-296

In the meantime, you can work around it using:

var query = new QueryDocument("$gt", 3);
var update = Update.Pull("field", (IMongoQuery) query);

or perhaps even:

var update = new QueryDocument("$pull", new BsonDocument("$field",
new BsonDocument("$gt", 3)));

I'll let you translate that to Powershell... :)

On Aug 3, 5:13 am, Roman Kuzmin <nightro...@gmail.com> wrote:
> Example athttp://www.mongodb.org/display/DOCS/Updating:

Roman Kuzmin

unread,
Aug 3, 2011, 11:03:28 AM8/3/11
to mongodb-user
Robert,

Thank you. It is not really a problem for me (I know how to work
around). I ask wondering whether I miss something or the driver does,
or perhaps this is by design (kind of).

Another update expression example and a similar question. Can we do
this using just C# driver builders?
{$bit : {field : {and : 5, or : 2}}}

Roman Kuzmin

unread,
Aug 3, 2011, 11:10:21 AM8/3/11
to mongodb-user
Never mind this question, yes, we can:

[MongoDB.Driver.Builders.Update]::BitwiseAnd('name',
5).BitwiseOr('name', 2)

gets

{ "$bit" : { "name" : { "and" : 5, "or" : 2 } } }
Reply all
Reply to author
Forward
0 new messages