问题标签 [mgo]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
12026 浏览

go - 使用 mgo 的非结构化 MongoDB 集合

我对 Go 很陌生。从我在 mGo 的示例中看到的情况来看,为了查询集合然后从中读取,您必须预定义将在结构中返回的数据。

在 PHP 中,文档被分配给一个数组。这是完美的,因为一条记录可能具有完全不同的一组键(可能不包含姓名或电话但包含电子邮件),我可以直接访问它而无需设置预定义的类/结构/变量。

有没有办法在 Go / mGo 中做同样的事情?

0 投票
1 回答
1911 浏览

mongodb - Go mgo不存储对象

使用mgo我无法存储任何有意义的数据。只有_id被存储

Mongo 中的结果只是 _id 字段 - 没有“Joe”的迹象。


在 Arch linux 上使用 go 1.1.2,MongoDB 2.4.6。

0 投票
1 回答
883 浏览

mongodb - 如何通过 golang 中的 mgo 在 mongo 中插入 math/big.Int

我有一个包含math/big.Int字段的结构。我想使用mgo将结构保存在mongodb中。在我的情况下,将数字保存为字符串就足够了。

我查看了可用字段的标签,没有任何接缝允许自定义序列化程序。我期待实现一个类似于encoding/json.Marshaler但我在文档中发现没有这样的接口的接口。

这是我想要的一个简单的例子。

谢谢!

0 投票
2 回答
1453 浏览

mongodb - mgo API - mgo、mgo/bson、mgo/txn 之间的区别

我最近开始学习 Go 并且到目前为止非常喜欢它。我想学习如何使用mgoMongo 驱动程序制作 REST API。

在网站上,有三个 API:mgomgo/bsonmgo/txn。他们的意思是什么?

0 投票
1 回答
1490 浏览

mongodb - mgo time.Time 或布尔检查

我有一个 mongo 文档,其中包含一个日期字段,该字段也可以是假的(或未定义),我似乎无法找到如何检查该字段是否可用或者是假的或者是日期(time.Time) golang/mgo :S

0 投票
1 回答
2946 浏览

mongodb - 如何表示具有混合类型的数组

我正在使用$substr来自 MongoDB 的命令构建一个聚合管道查询,但我不知道如何使用 mgo 驱动程序表示它在 Go 中所需的数组,因为它包含不同类型的值(字符串、整数)。

这是javascript中的查询:

这试图做的是获取dt(来自聚合的前一阶段)的子字符串,其起始索引为 0,结束索引为 6。

在围棋我得到:

["$dt",0,6]不是正确的表示,我尝试的一切似乎都失败了。

0 投票
2 回答
3190 浏览

go - Golang mgo 错误

mgo可以返回不同于 QueryError 或 ErrNotFound 的错误吗?数据库连接错误怎么办?

对不同于 ErrNotFound 的错误感到恐慌并在 http 处理程序堆栈的顶部恢复状态为 500 的漂亮响应是一种好习惯吗?

0 投票
1 回答
1645 浏览

mongodb - Mongo Connection Count creeping up one per 10 second with mgo driver

We monitor our mongoDB connection count using this:

http://godoc.org/labix.org/v2/mgo#GetStats

However, we have been facing a strange connection leak issue where the connectionCount creeps up consistently by 1 more open connection per 10 seconds. (That's regardless whether there is any requests). I can spin up a server in localhost, leave it there, do nothing, the conectionCount will still creep up. Connection count eventually creeps up to a few thousand and it kills the app/db then and we have to restart the app.

This might not be enough information for you to debug. Does anyone have any ideas, connection leaks that you have dealt with in the past. How did you debug it? What are some of the way that I can debug this.

We have tried a few things, we scanned our code base for any code that could open a connection and put counters/debugging statements there, and so far we have found no leak. It is almost like there is a leak in a library somewhere.

This is a bug in a branch that we have been working on and there have been a few hundred commits into it. We have done a diff between this and master and couldn't find why there is a connection leak in this branch.

As an example, there is the dataset that I am referencing:

MasterConns is the number that creeps up one per 10 second. I am not entirely sure what the other numbers can mean.

0 投票
1 回答
353 浏览

interface - Golang 接口来简化依赖?

嗯,我在处理接口时遇到了问题。

所以我使用 Go 包来处理我的 mongodb 东西,但我不想将该包导入到每个模型中,等等。我想将尽可能多的子包(如模型)保留在标准库中。所以我想我会像这样布置一些接口:

问题是,当我去使用这样的功能时:

通过传入我的 mgo.Database 在使用接口的包中找到:

我得到一个编译器错误:

controllers/handlers.go:35: 不能在函数参数中使用 ctx.Database (type *mgo.Database) 作为类型 dota.database: *mgo.Database 没有实现 dota.database (C 方法的错误类型) 有 C(string ) *mgo.Collection 想要 C(string) dota.collection

我对这个概念缺少什么?

0 投票
1 回答
5505 浏览

unit-testing - 单元测试 golang 处理程序

这是我编写的用于从 mongodb 检索文档的处理程序。
如果找到文档,我们将相应地加载和渲染模板。如果失败,它将重定向到 404。

手动尝试一下,一切都很好。

但是,我似乎无法通过我的单元测试。在相应的测试文件中,这是我尝试加载我的EventNextHandler.

测试在说明 的行处失败EventNextHandler(response, request)

在我的错误消息中,它指的是err := db.Find(&Event{}, search).Sort(sort).One(&result)我的处理程序代码中的行。

在此处完成错误消息:-

编写测试的正确方法是什么?考虑到从 mongodb 中没有检索到任何内容的情况,并应用断言来验证这一点,从而编写一个经过验证的测试。