- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我使用 Golang 作为我的后端,使用 MongoDB 作为我的 Web 应用程序的数据库。我需要添加两个值并检查数据库中的值是否大于添加的值,如果是,我需要从 MongoDB 中删除该行。
我写的代码如下
err, err1 := c.RemoveAll(bson.M{
"currenttime": bson.M{
"$gt": bson.M{
"$add": []interface{}{time.Now().Unix(),"100"},
},
},
})
仅供引用:我为 MongoDB 编写了示例代码,但效果不佳
db.Collection.remove(
{currenttime:{
$gt:{
$add:[100,100]
}
}
}
)
如果可行,请告诉我。请给我任何替代方法。
谢谢
最佳答案
1- 如果您需要删除带有字段的行,例如time > t + 10
来自MongoDB,你不需要$add
,你可以使用:
info, err := c.RemoveAll(bson.M{"time": bson.M{"$gt": t + 10}})
if err != nil {
panic(err)
}
这是工作代码:
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"`
}
输出:
[{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-对于$add
,参见:
Query where sum of two fields is less than given value
https://docs.mongodb.com/manual/reference/operator/aggregation/add/
关于mongodb - 需要使用 $add 和 $gt 删除使用 mgo (Golang && MongoDB),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39908192/
我最近开始学习 Go,到目前为止我非常喜欢它。我想了解如何使用 mgo Mongo 驱动程序制作 REST API。 在网站上,共有三个API:mgo、mgo/bson、mgo/txn。它们是什么意思
我有一个 GoLang 代码: c.Find(selectQuery).All(&results) if err == mgo.ErrNotFound { // error handling } se
我有一组动态的成分名称,并为每个用户提供。我想将它与 mongo 文档匹配,其中有一个名为 ingredients 的对象的 array 具有属性 name。我已经编写了一个查询(见下文),它将从 U
我 我很难将 MongoDB 查询转换为 mgo bson。 Mongo 记录模式如下所示。我想查找主题标签为“教育”和“学生”的记录。 db.questions.insert ( {
我正在尝试使用 mgo 库进行批量更新插入。我正在阅读 documentation关于批量更新插入,因为这是我第一次使用 MongoDB,看起来我必须提供成对的文档才能更新。 在我的函数中,我正在执行
我一直在为我的 API 使用 mgo,但我在我的 MongoDB 中看到许多当前连接(同时使用少于 5 个设备进行测试)。通过在我的 Mongo 服务器中执行 db.serverStatus().co
我正在尝试检索以下数据中每个 object_name 的最早创建日期和最后修改日期 { "_id" : ObjectId("5a510666b2e543371cff44ef"), "object_na
我是 Golang 的新手,我正在使用 mgo 框架构建 Golang CRUD 来练习。当我使用 mgo 插入一个元素时,插入了两次而不是一次,我在 MongoDB Compass 社区中检查了它。
我正在从事一个基于 MongoDB 数据结构的项目。我们存储在数据库中的对象如下所示: { "_id" : ObjectId("567a877df1c7720bea7c2f51"), "usernam
我正在尝试使用 golang mgo 执行查询为了有效地从连接中获得不同的值,我知道这可能不是在 Mongo 中使用的最佳范例。 像这样: pipe := []bson.M{ {
我的目标是实现这个 code .除了不使用 sql,我想使用 mongoDB。我认为我处理 session 的方式存在问题。 我正在尝试使用 mgo 通过 Rest API 将一些用户数据插入到 Mo
我正在尝试使用 Go 中的 mgo 库实现以下功能: db.artists.update( {_id: ObjectId("534944125117082b30000001")}, {
我的 Go 应用程序已使用 MGO 连接到 MongoDB(托管在 Compose.io 上),没有任何问题。 今天我决定删除这个数据库并添加一个不同的数据库(再次使用 Compose)。我更新了连接
我有这个模型数据,我用它来将数据保存到数据库中 type Nos struct { UnitCode string `json:"unitCode" bson:"unitCode
我正在用 mgo 编写一个数据库接口(interface)。我的模型中的某些文档引用了其他文档。 type Child struct{ Id bson.ObjectId `js
我有一个查询,它从包含特定文本的集合文档中返回所有名称。在以下示例中,不区分大小写地返回所有包含序列“oh”的名称;不返回文档中的其他字段: find({name:/oh/i}, {name:1, _
我使用以下 go 文件作为我的 http API 和 mgo 之间的层: package store import ( "reflect" "strings" "labix.o
我正在编写一个快速写入 mongodb 的应用程序。 mongodb 和 mgo 处理得太快了。我的问题是,有没有办法让我确定 mongo 跟不上并开始阻塞?但我也不想无谓地阻止。以下是模拟问题的代码
我有以下代码: competitionMatch := bson.M{ "$match": bson.M{"competition" : bson.M{"$in" : []string{"PREMIE
下面我设计了文档结构如下: type MIS_Course struct { ID bson.ObjectId `bson:"_id,omitempty"` Name string
我是一名优秀的程序员,十分优秀!