通过使用golang在具有多个条件的mongo查询中发布 [英] Issue in mongo query with multiple conditions by using golang

查看:917
本文介绍了通过使用golang在具有多个条件的mongo查询中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下文件-

{ 
    "_id" : "580eef0e4dcc220df897a9cb", 
    "brandId" : 15, 
    "category" : "air_conditioner", 
    "properties" : [
        {
            "propertyName" : "A123", 
            "propertyValue" : "A123 678"
        }, 
        {
            "propertyName" : "B123", 
            "propertyValue" : "B123 678"
        }, 
        {
            "propertyName" : "C123", 
            "propertyValue" : "C123 678"
        }
    ]
}

在这种情况下,properties数组可以具有多个元素. 当我通过API执行搜索时, 理想情况下,我会在POST请求的正文中传递类似于properties的数组-

In this, the properties array can have multiple number of elements. When I perform a search via my API, I would ideally pass an array similar to properties in the body of my POST request -

{ 
    "brandId" : 15, 
    "category" : "air_conditioner", 
    "properties" : [
        {
            "propertyName" : "A123", 
            "propertyValue" : "A123 678"
        }, 
        {
            "propertyName" : "B123", 
            "propertyValue" : "B123 678"
        }, 
        {
            "propertyName" : "C123", 
            "propertyValue" : "C123 678"
        }
    ]
}

我有一个接收和解码此信息的结构-

I have a struct to receive and decode this information -

type Properties struct {
    PropertyName  string `json:"propertyName" bson:"propertyName"`
    PropertyValue string `json:"propertyValue" bson:"propertyValue"`
}

type ReqInfo struct {
    BrandID      int             `json:"brandId" bson:"brandId"`
    Category     string          `json:"category" bson:"category"`
    Properties   []Properties    `json:"properties" bson:"properties"`
}

我还可以对各种properties执行mongodb $and操作,并且只有当所有这些匹配时,才返回文档. 这里的问题是properties数组中的元素数量不固定. 我需要能够只发送

I can also perform a mongodb $and operation over the various properties and only when all of them match, the document is returned. The problem here is that the number of elements in the properties array is not fixed. I need to be able to send just

{ 
    "brandId" : 15, 
    "category" : "air_conditioner", 
    "properties" : [
        {
            "propertyName" : "A123", 
            "propertyValue" : "A123 678"
        }
    ]
}

并检索所有匹配的文档(不仅仅是一个).

and retrieve all the matching documents (not just one).

我尝试使用for循环创建可变大小的bson.M,具体取决于接收作为输入的properties数组的大小,但是找不到正确的方法!

I tried my hand at creating a variable size bson.M using a for loop depending on the size of the properties array being received as the input but couldn't get the right way to do it!

应该如何处理?

推荐答案

我能够通过分别构造$and部分来实现这一目标-

I was able to achieve this by constructing the $and part separately -

var AndQuery []map[string]interface{}
for i := 0; i < len(body.Properties); i++ {
    log.Println(body.Properties[i])
    currentCondition := bson.M{"properties": bson.M{"$elemMatch": bson.M{"propertyName": body.Properties[i].PropertyName, "propertyValue": body.Properties[i].PropertyValue}}}
    AndQuery = append(AndQuery, currentCondition)
}

然后我的查询看起来像-

and then my query looked like -

c.Find(bson.M{"brandId": body.BrandID, "category": body.Category, "$and": AndQuery})

这篇关于通过使用golang在具有多个条件的mongo查询中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆