mongodb - 获取具有多个条件的 MongoDB 集合记录
问题描述
我想获取具有多个条件的 mongodb 集合,但出现错误:
panic: Failed to parse: filter: [ { visibility: { $eq: "4" } }, {
discontinued: { $ne: "1" } }, { status: { $eq: "1" } } ]. 'filter' field
must be of BSON type Object.
代码如下:
package main
import (
"fmt"
"gopkg.in/mgo.v2/bson"
)
func GenerateFeed(headers, attributes interface{}, conditions
[]interface{}) {
var operations = []bson.M{}
for _, val := range conditions {
var attr, operator, value interface{}
cons := val.(map[interface{}]interface{})
for range cons {
attr = cons["attribute"]
operator = cons["operator"]
value = cons["value"]
switch operator {
case "==":
operator = "$eq"
case "!=":
operator = "$ne"
case "()":
operator = "$in"
}
}
operations = append(operations, bson.M{attr.(string):
bson.M{operator.(string): value}})
}
var products []Prod
session := Connect()
collection := session.DB("rfgv2").C("catalog_product_entity_1")
err := collection.Find(operations).All(&products)
CheckError(err)
fmt.Println(products)
}
type Prod struct {
EntityId string `bson:"entity_id"`
Name string `bson:"name"`
TypeId string `bson:"type_id"`
Sku string `bson:"sku"`
Manufacturer int32 `bson:"manufacturer"`
Status int32 `bson:"status"`
Visibility int32 `bson:"visibility"`
EnableGoogleCheckout int32 `bson:"enable_google_checkout"`
Delivery string `bson:"delivery"`
MetaTitle string `bson:"meta_title"`
MetaDescription string `bson:"meta_description"`
Image string `bson:"image"`
SmallImage string `bson:"small_image"`
Thumbnail string `bson:"thumbnail"`
Gallery string `bson:"gallery"`
UrlKey string `bson:"url_key"`
UrlPath string `bson:"url_path"`
Mpn string `bson:"mpn"`
ProductListingAds string `bson:"product_listing_ads"`
Color string `bson:"color"`
Price float32 `bson:"price"`
Cost float32 `bson:"cost"`
TierPriceForBundle float32 `bson:"tier_price_for_bundle"`
RegularPrice float32 `bson:"regular_price"`
SpecialFromDate string `bson:"special_from_date"`
Description string `bson:"description"`
MetaKeyword string `bson:"meta_keyword"`
Dimensions string `bson:"dimensions"`
Features string `bson:"features"`
DeliveryPopupMessage string `bson:"delivery_popup_message"`
CreatedAt string `bson:"created_at"`
UpdatedAt string `bson:"updated_at"`
}
我正在尝试使用 go 从 mongodb 获取基于多个聚合函数的记录。基本上,如果我们删除方括号并尝试使用 mongo 命令它可以工作,但是如何在 go 中解决这个问题?
谢谢你。
解决方案
问题 vas 您将 声明operations
为 a []bson.M{}
,因此您将获得一组地图。
如果您检查 bson.M{} 的定义,它是一个 map[string]interface{} 您只需向地图添加元素(在您的情况下是您想要的操作)。为此,语法是yourMap[yourKey] = yourValue
.
尝试以下代码生成操作循环:
operations := bson.M{}
for _, val := range conditions {
var attr, operator, value interface{}
cons := val.(map[interface{}]interface{})
for range cons {
attr = cons["attribute"]
operator = cons["operator"]
value = cons["value"]
switch operator {
case "==":
operator = "$eq"
case "!=":
operator = "$ne"
case "()":
operator = "$in"
}
}
operations[attr.(string)] = bson.M{operator.(string): value}
}
推荐阅读
- elasticsearch - Elasticsearch circle - geo_shape 查询没有给出我预期的结果
- openstack - 如何在 openstack kolla 中使用 OVN?
- sql-server - 为什么平面文件未在 SSIS 源中加载?
- sql - Select 语句创建序列变量
- pine-script - 如何克服 Pine Script 中的“编译的脚本代码太长”
- linux - 在 Cron 作业中,GET、PHP 和 cURL 有什么区别
- r - 如何在一张表中返回多个结果?
- azure - Azure DevOPS - 在分配工作项时收到通知
- java - AWS Lambda 未在层中找到共享对象文件
- c - 为什么我在 c 中的矩阵乘法代码总是给出垃圾值?(使用共享内存和fork)