- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我试图将游标的数据解码成一个map[string]interface{},我直接试了但是它根本不起作用,所以我发现我必须将它转换为BSON文档并且接下来将其转换为 map[string]interface{},最后转换为 JSON 字符串。我尝试了以下代码:
...
for cursor.Next(context.Background()) {
err = cursor.Decode(&itemBson)
...
b, err := bson.Marshal(itemBson)
...
err = bson.Unmarshal(b, &itemMap)
...
}
...
但是bson文档有如下值:
map[_id:ObjectID("5c2d0809a49bad7d547ec028") applications:bson.Array[bson.Document{bson.Element{"enabled": true}}] userName:coto userUUID:df2dea92-c189-53b3-aafe-485d0be23bee]
并将 map 解析为 JSON:
{"_id":"5c2d0809a49bad7d547ec028","applications":[{}],"userName":"coto","userUUID":"df2dea92-c189-53b3-aafe-485d0be23bee"
如您所见,键“applications”在 JSON 中是空的,但它在 BSON 文档中确实有内容。我不知道为什么数据会消失。
我该如何解决这个错误?谢谢。
最佳答案
已解决:
我使用以下代码解决了这个错误:
var jsonDocuments []map[string]interface{}
var byteDocuments []byte
var bsonDocument bson.D
var jsonDocument map[string]interface{}
var temporaryBytes []byte
for cursor.Next(context.Background()) {
err = cursor.Decode(&bsonDocument)
...
temporaryBytes, err = bson.MarshalExtJSON(bsonDocument, true, true)
...
err = json.Unmarshal(temporaryBytes, &jsonDocument)
...
jsonDocuments = append(jsonDocuments, jsonDocument)
}
关于mongodb - 我如何将 BSON 文档转换为 map[string]interface{},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54133724/
我试图理解 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)
我是一名优秀的程序员,十分优秀!