doubi7346
2018-06-27 18:45
浏览 165

使用Golang动态查询mongodb

I'm trying to query my mongodb database using golang (and the mgo library) with only one function, and the method I am currently using is:

er = c.Find(sel(items)).Sort("-createdAt").All(&result)

Where items is a map and the key is the name of the field I am searching inthe db, and the value is what I want to search by.

and sel() is:

func sel(query map[string]string) bson.M {
result := make(bson.M, len(query))
result[ ] = "$in"
for k, v := range query {
    result[k] = v
}
return result

currently it will return all of the results where at least one of the fields matches the input map. (So a logical OR) however I would like it to return the logical AND of these fields.

Does anyone have suggestions on how to modify the existing code or a new way of efficiently querying the database?

Thank you

1条回答 默认 最新

相关推荐 更多相似问题