MongoDB 在 golang 中使用 $or 和 $and 的组合查找查询

标签 mongodb go

我想在以下位置获取行:

{repair field has "ac" OR {repair is "tv" and phone field in range 1091-1100}}

我正在尝试以下查询:

type M map[string]interface{}
conditions := M{"name": M{"$regex": "me"},
    "$or": []M{M{"repair": M{"$eq": "ac"}},
"$and": []M{M{"repair": M{"$eq": "tv"}}, M{"phone": M{"$gte": 1091, "$lte": 1100}}}}}
    fmt.Println(conditions)
    err = c.Find(conditions).Sort("phone").Limit(20).All(&j)

但是,我遇到了一个编译错误:

index must be non-negative integer constant
cannot use []M literal (type []M) as type M in array or slice literal.

最佳答案

您在 "$and" 之前缺少一个 M{ 并且在添加之后不要忘记添加另一个右括号 }.

the goodthe bad进行比较。

关于MongoDB 在 golang 中使用 $or 和 $and 的组合查找查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46194546/

相关文章:

javascript - gorilla /csrf 的问题

arrays - Golang 对二维数组进行排序

mongodb - 具有多个条件的聚合 Switch Case 语法

date - 如何在 Go 语言中生成随机日期?

Go:使用 strconv.ParseFloat 将字符串转换为 float 返回 0

java - 如何将256个元素的数组检索到mongodb?

go - 在Go中附加为指针的局部变量的生命周期

html - 如何在 HTML 中显示 ImageGridFSProxy?

node.js - 使用 Mongoose 在投影中包含 Mongodb "_id"字段

mongodb - 如何使用本地 mongodump 数据恢复远程 MongoDB 服务器