如何在 Golang 的 mgo 查询中 运行 $and 运算符
How to run $and operator in mgo query in Golang
我想在 GolangMongoDB 中执行以下查询
check_select = bson.M{
"$and": []interface{}{
"shr_key": user_shr_key,
"id": uid,
"user_history": bson.M{"$elemMatch": bson.M{"action": "STOP", "message_id": mid}},
},
}
请帮忙...我收到以下错误 "index must be non-negative integer constant"
。
错误来自您在 go
中初始化 array
的方式:
....
"$and": []interface{}{
"shr_key": user_shr_key,
....
go
数组不接受 string
作为索引。
无论如何,为了解决你的问题,从数组初始化中删除索引并将键值对包装在 bson.M
中即可,例如:
bson.M{
"$and": []bson.M{ // you can try this in []interface
bson.M{"shr_key": user_shr_key},
bson.M{"id": uid},
bson.M{"user_history": bson.M{"$elemMatch": bson.M{"action": "STOP", "message_id": mid}}},
},
}
在这里您可以看到,通过 $Match、$ 和使用 Golang 获取值 MongoDB
pipeline := []bson.M{
bson.M{"$match": bson.M{"$and": []bson.M{bson.M{"storyID": storyID},
bson.M{"parentID": parentID}}}}
}
我想在 GolangMongoDB 中执行以下查询
check_select = bson.M{
"$and": []interface{}{
"shr_key": user_shr_key,
"id": uid,
"user_history": bson.M{"$elemMatch": bson.M{"action": "STOP", "message_id": mid}},
},
}
请帮忙...我收到以下错误 "index must be non-negative integer constant"
。
错误来自您在 go
中初始化 array
的方式:
....
"$and": []interface{}{
"shr_key": user_shr_key,
....
go
数组不接受 string
作为索引。
无论如何,为了解决你的问题,从数组初始化中删除索引并将键值对包装在 bson.M
中即可,例如:
bson.M{
"$and": []bson.M{ // you can try this in []interface
bson.M{"shr_key": user_shr_key},
bson.M{"id": uid},
bson.M{"user_history": bson.M{"$elemMatch": bson.M{"action": "STOP", "message_id": mid}}},
},
}
在这里您可以看到,通过 $Match、$ 和使用 Golang 获取值 MongoDB
pipeline := []bson.M{
bson.M{"$match": bson.M{"$and": []bson.M{bson.M{"storyID": storyID},
bson.M{"parentID": parentID}}}}
}