I have a collection of users. Each of those have an array of bookmarks. Each bookmark has an array of categories it belongs to. Leading to a structure like this:
[
{name: "Bob",
bookmarks: [
{url: "http://duckduckgo.com",
categories: [
"Search",
"Ducks",
],
},
],
},
]
Now given a name and a url and a category name. I want to delete said category of the respective bookmark. But my problem is that all attempts return the whole user or delete the whole bookmark and not just the category.
This is my best attempt using the mgo driver so far:
type arbitraryJson map[string]interface{}
user := "Bob"
bookmarkURL := url.Parse("http://duckduckgo.com")
tagName := "Search"
err = userDB.Update(
arbitraryJson{
"name": user,
"bookmarks.url": bookmarkURL.String(),
},
arbitraryJson{
"$pull": arbitraryJson{
"bookmarks.categories": tagName,
},
},
)
Which translates (I think) to the mongo query:
db.users.updateOne(
{ name: "Bob",
bookmarks.url: "http://duckduckgo.com" }
{
$pull: { bookmarks.categories: "search" }
}
)