go - How do I remove an array item in Mongodb / Golang? -


i have following data structure, , i'm attempting remove item 'artists' array.

[     {         "id": "56b26eeb4a876400011369e9",         "name": "ewan valentine",         "email": "ewan@test.com",         "artists": [             "56b26f334a876400011369ea",             "56b2702881318d0001dd1441",             "56b2746fdf1d7e0001faaa92",         ],         "user_location": "manchester, uk"     } ] 

here's function...

// remove artist user func (repo *userrepo) removeartist(userid string, artistid string) error {     change := bson.m{"artists": bson.m{"$pull": bson.objectidhex(artistid)}}     fmt.println(userid)     err := repo.collection.updateid(bson.objectidhex(userid), change)     return err } 

i'm getting following error:

{   "_message": {     "err": "the dollar ($) prefixed field '$pull' in 'artists.$pull' not valid storage.",     "code": 52,     "n": 0,     "waited": 0,     "fsyncfiles": 0,     "wtimeout": false,     "updatedexisting": false,     "upsertedid": null   } } 

the $pull operator "top level" operator in update statements, have wrong way around:

    change := bson.m{"$pull": bson.m{"artists": bson.objectidhex(artistid)}} 

the order of update operators operator first, action second.

if there no operator @ "top level" keys, mongodb interprets "plain object" update , "replace" matched document. hence error $ in key name.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -