duai0935 2016-10-07 01:56
浏览 436
已采纳

需要使用mgo在删除中使用$ add和$ gt(Golang && MongoDB)

I'm using Golang as my backend and MongoDB as the database for my web application. I'm in need of adding two values and check if the value in the database is greater than the added value and if it is, I need to remove the row from the MongoDB.
The code which I wrote is as follows

err, err1 := c.RemoveAll(bson.M{
        "currenttime": bson.M{
            "$gt": bson.M{
                "$add": []interface{}{time.Now().Unix(),"100"},
            },
        },
     })

FYI: I wrote the sample code for MongoDB but this is not working as well

db.Collection.remove(
   {currenttime:{
        $gt:{
                $add:[100,100]
             }
              }
    }
 )

Please let me know if this is possible. And please give me any alternative way to do this.
Thanks

  • 写回答

4条回答 默认 最新

  • doufu1950 2016-10-07 05:44
    关注

    1- If you need to remove rows with field e.g. time > t + 10 from the MongoDB, you do't need $add, you may use:

    info, err := c.RemoveAll(bson.M{"time": bson.M{"$gt": t + 10}})
    if err != nil {
        panic(err)
    }
    

    Here is the working code:

    package main
    
    import (
        "fmt"
        "time"
    
        "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("Model")
        c.DropCollection()
        t := time.Now().UTC().Unix()
        err = c.Insert(
            &Model{bson.NewObjectId(), "n1", t},
            &Model{bson.NewObjectId(), "n2", t + 50},
            &Model{bson.NewObjectId(), "n3", t + 100},
            &Model{bson.NewObjectId(), "n4", t + 150})
        if err != nil {
            panic(err)
        }
        r := []Model{}
        err = c.Find(nil).All(&r)
        if err != nil {
            panic(err)
        }
        fmt.Println(r)
    
        info, err := c.RemoveAll(bson.M{"time": bson.M{"$gt": t + 10}})
        if err != nil {
            panic(err)
        }
        fmt.Println(info)
    
        r2 := []Model{}
        err = c.Find(nil).All(&r2)
        if err != nil {
            panic(err)
        }
        fmt.Println(r2)
    }
    
    type Model struct {
        ID   bson.ObjectId `json: "_id,omitempty" bson: "_id,omitempty"`
        Name string        `json: "name" bson: "name"`
        Time int64         `json: "time" bson: "time"`
    }
    

    output:

    [{ObjectIdHex("57f7354cc3176906c0ca7516") n1 1475818828} {ObjectIdHex("57f7354cc3176906c0ca7517") n2 1475818878} {ObjectIdHex("57f7354cc3176906c0ca7518") n3 1475818928} {ObjectIdHex("57f7354cc3176906c0ca7519") n4 1475818978}]
    &{0 3 3 <nil>}
    [{ObjectIdHex("57f7354cc3176906c0ca7516") n1 1475818828}]
    

    2- For $add, see:
    Query where sum of two fields is less than given value
    https://docs.mongodb.com/manual/reference/operator/aggregation/add/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化