2

I have been doing experiments on MongoDB. Collection are as follows(sample). Project this, in this class of 95th DeleteAlbum line have the function of.

{
    "_id": {
        "$oid": "50e34afce4b0bc114fea2a7e"
    },
    "Ad": "Tarkan", //Artist
    "Albums": [
        {
            "_id": {
                "$oid": "50e34afce4b0bc114fea2a4e"
            },
            "Isim": "DUDU", //Name
            "Yil": 2005, //Year
            "Resim": "http://www.gercekpop.com/wp-content/uploads/2003/12/tarkan-dudu.jpg" //Image
        },
        {
            "_id": {
                "$oid": "50e34afce4b0bc114fea2a3e"
            },
            "Isim": "Kuzu Kuzu",
            "Yil": 2008,
            "Resim": "http://o.scdn.co/300/bf6423177c32224f25dc742f3ffe5450e441d68d"
        }
    ]
}

line 75th Add method in CreateAlbum function are working.

 Artist.Albums.Add(Album)
 Return Collection.Save(Artist, SafeMode.True).Ok

but Remove method in DeleteAlbum not work.

Artist.Albums.Remove(album)
Return Collection.Save(sanatci, SafeMode.True).Ok

What should be the method to delete the embedded document? Thank you for your help.

B. Bilgin
  • 774
  • 12
  • 22
  • Use an `update` with the [`$pull`](http://docs.mongodb.org/manual/reference/operators/#_S_pull) operator to remove an element from an array field. – JohnnyHK Jan 13 '13 at 05:12
  • Hi @JohnnyHK. Do you have an example with mongodb csharp driver. – B. Bilgin Jan 13 '13 at 12:25

2 Answers2

4

Here's how you'd remove an element from the Albums array of the first document that contains it by its _id (in C#):

var query = Query.EQ("Albums._id", new ObjectId("50e34afce4b0bc114fea2a4e"));
var update = Update.Pull("Albums", new BsonDocument(){
    { "_id", new ObjectId("50e34afce4b0bc114fea2a4e") }
});
collection.Update(query, update);

I'm not fluent in VB, but your DeleteAlbum method should change to something like:

Public Function DeleteAlbum(Artist As Sanatci, album As Album) As Boolean
    Try
        Return Collection.Update(
            Query.EQ("Albums._id", album._id),
            Update.Pull("Albums", Query.EQ("_id", album._id)), SafeMode.True
        ).Ok
    Catch ex As MongoCommandException
        Dim msgLog As String = ex.Message
        Return False
    End Try
End Function
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
0

Mongo is not like Linq-to-SQL or Entity Framework. Instead of calling Artist.Albums.Remove(album) and then trying to save the new state of the object to the collection, you need to call the .Remove() function of the collection.

This article shows how you would do it in C#:

How to remove one 'document' by 'ID' using the Official C# Driver for MongoDB?

Community
  • 1
  • 1
ryan1234
  • 7,237
  • 6
  • 25
  • 36
  • 1
    I'm sorry I thought you wanted to remove the artist from the collection. If you just want to modify the album array for an object, then consider JohnnyHK's answer. You can upsert a new object (that will replace the existing object with a new one with your change), or remove the element from the array. I always prefer to upsert elements, which means inserting a new object if nothing exists with that _id - or updating the entire object if it already exists. – ryan1234 Jan 13 '13 at 16:56