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.

2
  • Use an update with the $pull operator to remove an element from an array field. Commented Jan 13, 2013 at 5:12
  • Hi @JohnnyHK. Do you have an example with mongodb csharp driver. Commented Jan 13, 2013 at 12:25

2 Answers 2

4
+50

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
Sign up to request clarification or add additional context in comments.

Comments

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?

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.