Drew
Drew

Reputation: 898

MongoDB: remove item from child collection

I'm trying to remove an item from my a child collection in my MongoDB document.

Here is a simple example:

{
    _id : ObjectId("4f966b41682dbc1be0c7b640"),
    Firstname: "John",
    Lastname: "Doe",
    Skills: [
        { 
          _id : ObjectId("4f966b3f682dbc1bec7b63c"),
          name: "C#"
        },
        { 
          _id : ObjectId("4f966b3f682dbc1bec7b63c"),
          name: "ASP.NET"
        },
    ]
}

I've tried $pull and it works fine on the shell, but how can I accomplish this using Linq.

any suggestion is much apprciated, thanks :)

Upvotes: 0

Views: 1225

Answers (1)

Craig Wilson
Craig Wilson

Reputation: 12624

Pull is an update mechanism and cannot be accomplished with Linq. However, you can drop down into native syntax to accomplish this from the .NET driver.

var update = Update.Pull("Skills");
var query = Query.Eq("_id", myObjectId);
collection.Update(query, update);

Hopefully, we'll get this to be more strongly typed in the future. You can see the documentation here: http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-Updatemethod.

Upvotes: 1

Related Questions