How to pass an int slice to “$in” using mgo

爷,独闯天下 提交于 2019-11-30 19:14:16

问题


I'm having a bit of trouble creating a query using the bson functionality of mgo. I'm simply trying to do {'search_id': {'$in': [1,2,4,7,9]}}, but I can't work out how to do it in mgo.

I have a slice of ints, and tried passing that directly:

toRemove := []int{1,2,4,7,9}
err = coll.Remove(bson.M{"search_id": bson.M{"$in": toRemove}})

I saw another post which suggested I needed to use []interface{}, but that doesn't work either:

toRemoveI := make([]interface{}, len(toRemove))
for idx, val := range toRemove {
    toRemoveI[idx] = val
}
err = coll.Remove(bson.M{"search_id": bson.M{"$in": toRemoveI}})

I've looked through he docs and other questions here and on gh, but most questions involving slices seem to be about getting data into a slice as opposed to what I'm trying to achieve.

Any help would be most appreciated.


回答1:


Your original proposal (passing an []int value) has no flaws, it's valid to do that.

What the problem is is that you use Collection.Remove() which finds and removes a single document matching the provided selector document. So your proposed solution will remove exactly 1 document, one whose search_id is contained in the slice you passed. If no such document is found (and the session is in safe mode, see Session.SetSafe()), mgo.ErrNotFound is returned.

Instead use Collection.RemoveAll() which finds and removes all documents matching the selector:

toRemove := []int{1,2,4,7,9}
info, err := c.RemoveAll(bson.M{"search_id": bson.M{"$in": toRemove}})

if err != nil {
    log.Printf("Failed to remove: %v", err)
} else {
    log.Printf("Removed %d documents.", info.Removed)
}


来源:https://stackoverflow.com/questions/42665941/how-to-pass-an-int-slice-to-in-using-mgo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!