Querying mongodb from golang using the _id stored in an array

前端 未结 2 397
广开言路
广开言路 2020-12-21 06:50

So here is my question. I have an array which are stored the _ids of mongodbs objects. Whats the right way to retrieve them all in one query using the mgo and b

2条回答
  •  离开以前
    2020-12-21 06:56

    MongoDB syntax for go.mongodb.org/mongo-driver has been updated, this should work using the official driver.

    oids := make([]primitive.ObjectID, len(ids))
    for i := range ids {
        objID, err := primitive.ObjectIDFromHex(ids[i])
        if err == nil {
            oids = append(oids, objID)
        }
    }
    

提交回复
热议问题