查询两个字段的总和小于给定值的位置 [英] Query where sum of two fields is less than given value

查看:181
本文介绍了查询两个字段的总和小于给定值的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Go语言和MongoDB以及 mgo.v2 驱动程序,并且我有像

I am using Go language and MongoDB with mgo.v2 driver and I have struct like

type MarkModel struct {
    ID          bson.ObjectId                   `json: "_id,omitempty" bson: "_id,omitempty"`
    Name        string                          `json: "name" bson: "name"`
    Sum         int                             `json: "sum" bson: "sum"`
    Delta       int                             `json: "delta" bson: "delta"`
}

我需要找到所有位于 Sum + Delta< 1000 为例。目前我加载所有,然后在Go代码中过滤,但我想过滤查询级别。

如何进行查询?

I need to find all where is Sum + Delta < 1000 for example. At the moment I load all and then in Go code I filter but I would like to filter on query level.
How to make that query ?

现在我全部返回

At the moment I return all with

marks := []MarkModel{}
c_marks := session.DB(database).C(marksCollection)
err := c_marks.Find(bson.M{}).All(&marks)
if err != nil {
    panic(err)
}

在这里我在for循环中过滤Go代码,但它不是最优的(这是不好的解决方案)。

and here I filter in Go code in for loop but it is not optimal ( it is bad solution ).

推荐答案

sum + delta < 1000 ,您可以使用:

To find all where is sum + delta < 1000, you may use:

pipe := c.Pipe(
    []bson.M{
        bson.M{"$project": bson.M{"_id": 1, "name": 1, "sum": 1, "delta": 1,
            "total": bson.M{"$add": []string{"$sum", "$delta"}}}},
        bson.M{"$match": bson.M{"total": bson.M{"$lt": 1000}}},
    })

这里是工作代码:

Here is the working code:

package main

import (
    "fmt"

    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

func main() {
    session, err := mgo.Dial("localhost")
    if err != nil {
        panic(err)
    }
    defer session.Close()
    session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
    c := session.DB("test").C("MarkModel")
    c.DropCollection()
    err = c.Insert(&MarkModel{bson.NewObjectId(), "n1", 10, 1}, &MarkModel{bson.NewObjectId(), "n2", 20, 2},
        &MarkModel{bson.NewObjectId(), "n1", 100, 1}, &MarkModel{bson.NewObjectId(), "n2", 2000, 2})
    if err != nil {
        panic(err)
    }

    pipe := c.Pipe(
        []bson.M{
            bson.M{"$project": bson.M{"_id": 1, "name": 1, "sum": 1, "delta": 1,
                "total": bson.M{"$add": []string{"$sum", "$delta"}}}},
            bson.M{"$match": bson.M{"total": bson.M{"$lt": 1000}}},
        })
    r := []bson.M{}
    err = pipe.All(&r)
    if err != nil {
        panic(err)
    }
    for _, v := range r {
        fmt.Println(v["_id"], v["sum"], v["delta"], v["total"])
    }
    fmt.Println()

}

type MarkModel struct {
    ID    bson.ObjectId `json: "_id,omitempty" bson: "_id,omitempty"`
    Name  string        `json: "name" bson: "name"`
    Sum   int           `json: "sum" bson: "sum"`
    Delta int           `json: "delta" bson: "delta"`
}



输出:

output:

ObjectIdHex("57f62739c22b1060591c625f") 10 1 11
ObjectIdHex("57f62739c22b1060591c6260") 20 2 22
ObjectIdHex("57f62739c22b1060591c6261") 100 1 101

这篇关于查询两个字段的总和小于给定值的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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