- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我需要将 MongoDB 查询传输到不同的系统。出于这个原因,我想使用 MongoDB Extended JSON .我需要这样做主要是因为我在查询中使用日期比较。
所以,问题的核心是我需要将在 node.js 后端生成的 MongoDB 查询传输到另一个用 Go< 编写的后端/em> 语言。
直觉上,通过 REST 发送此查询的最明显格式是 JSON。但是,MongoDB 查询不完全是 JSON,而是 BSON,它包含特殊的日期结构。
因此,我们的想法是使用 MongoDB Extended JSON 将查询转换为 JSON作为特殊构造的表示形式。经过一些测试后,很明显这些查询不起作用。 MongoDB shell 和通过 node.js 发送的查询都需要特殊的 ISODate
或 new Date
结构。
最后,真正的问题是:考虑到MongoDB Extended JSON,是否有从 JSON 编码/解码到 BSON 的函数? , 在 JavaScript (node.js) 和 Go 语言中?
显然 there is a node.js package解析和字符串化 BSON/JSON。这样,我的问题就解决了一半。我想知道 Go 语言中是否有类似的东西。
例如,以下查询在普通 BSON 中:
{ Tmin: { $gt: ISODate("2006-01-01T23:00:00.000Z") } }
翻译成MongoDB Extended JSON,就变成了:
{ "Tmin": { "$gt" : { "$date" : 1136156400000 }}}
最佳答案
经过一些研究,我找到了 mejson库,但它仅用于编码,所以我决定编写一个 Unmarshaller。
看ejson (我写的),现在它是一个非常简单的ejson
-> bson
转换器,没有bson
-> ejson
但是,您可以为此使用 mejson
。
安example :
const j = `{"_id":{"$oid":"53c2ab5e4291b17b666d742a"},"last_seen_at":{"$date":1405266782008},"display_name":{"$undefined":true},
"ref":{"$ref":"col2", "$id":"53c2ab5e4291b17b666d742b"}}`
type TestS struct {
Id bson.ObjectId `bson:"_id"`
LastSeenAt *time.Time `bson:"last_seen_at"`
DisplayName *string `bson:"display_name,omitempty"`
Ref mgo.DBRef `bson:"ref"`
}
func main() {
var ts TestS
if err := ejson.Unmarshal([]byte(j), &ts); err != nil {
panic(err)
}
fmt.Printf("%+v\n", ts)
//or to convert the ejson to bson.M
var m map[string]interface{}
if err := json.Unmarshal([]byte(j), &m); err != nil {
t.Fatal(err)
}
err := ejson.Normalize(m)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", m)
}
关于json - 向不同系统发送 MongoDB 查询 : converting to JSON and then decoding into BSON? Go 语言如何实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25218061/
我试图理解 BSON 符号 来自网站 BSON Site .但是,我无法理解相关性背后的原因。 我也提到了以下问题,但由于以下原因,我不相信。 Question 1 : 不熟悉 ruby 实现 Que
我收到以下错误: { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson:
假设我们有一个字符串,里面有二进制 bson 数据。如何加载到 bson object ? 最佳答案 我想在同一个页面上有一个例子可以做到这一点: BSONObjBuilder b; b << "na
我正在寻找如下所示的 type_of 方法: import bson bson.type_of(42) # it should return "int". bson.type_of("hello")
Base64 编码的 BSON 比 BSON 小吗? 最佳答案 Piskvor 的权利,base64 编码的任何东西都比原始长。您对某些内容进行 base64 编码以使其进入具有有限字符轨道的 cha
目前正在做一个 Golang 项目,但我得到了一些 Controller package controller import ( "go.mongodb.org/mongo-driver
这个特定的问题与使用 mongodb 和 golang 包 mongo-driver 相关。 ,但我认为这适用于与 mongodb 的大多数接口(interface)。 使用 Find 时要从集合中查
目前正在做一个 Golang 项目,但我得到了一些 Controller package controller import ( "go.mongodb.org/mongo-driver
可能相关:How to use interface type as a model in mgo (Go)? 我有一个像这样的结构: type Game struct { ID b
有没有一种方法可以使用 MongoDB C++ 驱动程序中的 BSON() 宏来生成 BSON 将空值。例如,为了生成一个 BSON 来表示这个文档:{"a": "foo", "b": null}:
我通过以下两种不同的方法生成了一个 ObjectId: user@ubuntu:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
我如何转换 BsonDocument到 FilterDefinition实例? 它是新的 MongoDb C# 驱动程序提供的类。 最佳答案 BsonDocument 和 FilterDefiniti
我正在尝试从 Json 生成 Bson。我尝试使用 Json.Net,但似乎有记录的行为,其中库为整数字段生成 uint64。不幸的是,我们必须使用 uint32。 因此我正在尝试使用 mongodb
我正在使用 https://github.com/mongodb/mongo-go-driver和目前正在尝试实现此类结构的部分更新 type NoteUpdate struct { ID
我目前正在尝试读取 bson 文件以将其导入数据库。我已经可以读取该文件并将其作为字节打印,但最终只收到 bson.errors.InvalidBSON: objsize Too Large 错误。
我尝试在 arch linux 迷你计算机(cubox)上安装 mean.io 堆栈。所以我安装了 nodejs 和 mongodb 包。 我用 Git 检索了堆栈,进行了 npm 安装(没问题)但是
为什么会输出false?我期待 true... package main import ( "fmt" "time" "gopkg.in/mgo.v2/bson" )
这是一种愚蠢的语法错误,尝试了很多方法,但都无法正常工作,请大家帮忙。 使用 mgo 在 Go 中使用 MongoDB,我只是尝试简化 $ne 运算符的使用,代码如下所示,但不断出现编译语法错误: l
我设置了新的 Typescript/React 项目,在 tsconfig.json 文件中收到此错误消息 "找不到 'bson' 的类型定义文件。该文件在程序中,因为:隐式类型库 'bson' 的入
我尝试将数据从 SQL Server 迁移到 MongoDB,但在将数据导入到 MongoDB 的最后阶段遇到了以下类型错误。 mongoImp = dbo.insert_many(jArray)
我是一名优秀的程序员,十分优秀!