I have RESTful API built on top of a MongoDB store, so it's nice that you can store arrays. It's straightforward to create a new resource like this:
POST /users
{
items: [
1001, 1002, 1003
]
}
But how would the HTTP endpoint for adding a new item or removing an item would look like?
Right now, I have to specify the entire array, including elements that I don't want to touch:
PATCH /users/{id}
{
name: 'Bruce Wayne',
items: [
1001, 1002
]
}
Or pass in a mongodb query directly:
PATCH /users/{id}?query[$push][items]=1003
Is there a better way to do this?
Edit:
I like how StackMob's API does it. How do I update the name
and remove an element from items
at the same time though? For example, when I'm updating a bunch of the user's details on an admin dashboard? I don't think replacing the entire array is a good idea in mongodb?