我有一个大小为 62,292,248 字节的 BSON 文档,我想分析它。我正在使用以下内容:
import bson
f = open('mybson.bson','rb')
s = f.read()
b = bson.loads(s)
f.close()
Run Code Online (Sandbox Code Playgroud)
在控制台上,当我输入b并按 Enter 时,即使s已读取所有字节,我也只能看到一个文档。我也尝试过f.read(62292248);但是,b好像只有一份文件!
我缺少什么?
这里有一个微观问题,关于为什么我想出的最终答案中需要向上转换(在本文的底部);以及一个关于我是否错过了“房间里的大象”的宏观问题:一些非常明显的简洁方式来做我想做的事[请不要问我-为什么-我想要我真正想要的;只要把它当作我想要这个的给定,它就是......]
我想通过 MongoDB.Bson CLR 程序集从 F# 初始化 BsonDocument。我认为我应该使用的 BsonDocument 构造函数的特定重载是
MongoDB.Bson.BsonDocument.BsonDocument(IDictionary<string,object>)
Run Code Online (Sandbox Code Playgroud)
这就是为什么我认为这是我应该使用的(以下是在类型花园中的漫长漫步......)
MongoDB 站点MongoDB CSharp 驱动程序教程中的 C# 示例使用集合初始值设定项语法,该语法映射到 BsonDocument 公开的接口上的 .Add 的一个或多个调用。教程示例类似于以下内容:
var bdoc = new BsonDocument { { "a", "1" }, { "b", "2" }, };
Run Code Online (Sandbox Code Playgroud)
我不确定正在使用 .Add 的哪个重载(并且不知道如何在 Visual Studio 中检查),但所有基于字典的重载都键入为 <string, object>。在这种情况下,每对中的第二个值,即字符串类型的“1”和“2”,自动(通过继承)也是对象类型,因此一切正常。.Add 的其他重载要求第二项的类型为 BsonValue,它是 BsonString 的抽象超类型,无论使用哪种重载,它都具有从 .NET 字符串的隐式转换;所以那里也一切都好。调用构造函数的哪个重载并不重要。
这有点难以转化为 F# 等效项,因为很难获取 BsonDocument 的 .Add 方法。我想到了
[("a", "1");("b", "2");] |> Seq.iter BsonDocument.Add
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为 BsonDocument.Add 不是静态方法;我可以实例化 BsonDocument,然后编写一个调用 BsonDocument 的 .Add 方法的 fun lambda,这至少会隔离 fun 的可变性:
[("a", "1");("b", "2");] …Run Code Online (Sandbox Code Playgroud) 这是我的集合的结构部分:
{
...
list: [
{ id:'00A', name:'None 1' },
{ id:'00B', name:'None 2' },
],
...
}
Run Code Online (Sandbox Code Playgroud)
您可以建议我使用C lib检索"id"和/或"name"字段中的值列表,请使用哪种方法?
我将如何使用 pymongo 仅返回 BSON ObjectId 的字符串组件。我可以通过从 bson.objectid 导入 ObjectId 将字符串编码为对象 ID;但我无法做相反的事情。
当我尝试:
for post in db.votes.find({'user_id':userQuery['_id']}):
posts += post['_id'].str
Run Code Online (Sandbox Code Playgroud)
我得到一个 ObjectId 没有属性 str 错误。
谢谢!
I am trying to import a sample BSON file test.bson into MongoDB. The server is already running, so I use
mongoimport --db test --collection foo --drop --file test.bson
Run Code Online (Sandbox Code Playgroud)
However, I get the following error:
2016-01-24T13:51:06.126-0500 connected to: localhost
2016-01-24T13:51:06.144-0500 Failed: error processing document #1: invalid character 'è' looking for beginning of value
2016-01-24T13:51:06.144-0500 imported 0 documents
Run Code Online (Sandbox Code Playgroud)
(1) How does one get around this error, invalid character 'è' looking for beginning of value? What exactly should I do to access …
使用 pymongo 客户端从 MongoDB 获取记录的代码段
from bson.json_util import dumps
cursor = db.collections.find({"Test": "Sample"})
for msg in cursor:
json_msg = dumps(msg)
Run Code Online (Sandbox Code Playgroud)
但是, json_msg 是字符串类型。有没有办法获得可以在 dict 之类的东西中遍历的 JSON 对象?我想在遍历它们时处理检索到的 JSON 对象。
我试过了json.loads(json_msg),但这又将 json_msg 转换回 BSON 格式。
编辑:我不只是想打印(这可以通过打印字符串来实现);但是,要迭代和处理 JSON 对象。
我正在使用 MongoDB 4.0。
我的集合中的文档可以有一个numbers包含整数数组的字段。我们可以numbers使用以下代码验证它确实是一个数组,但是否可以确保它们是整数?
properties: {
numbers: {
bsonType: 'array'
}
}
Run Code Online (Sandbox Code Playgroud)
我查看了验证文档和BSON 类型文档,但其中任何一个都没有说明任何内容。一个示例显示了括在方括号 ( ["double"])中的字段,但该类型并未描述为数组,并且添加方括号似乎没有任何效果。
我正在使用这样的结构
type User struct {
Username string `json: "username" bson: "username"`
FirstName string `json: "firstName" bson: "firstName"`
LastName string `json: "lastName" bson: "lastName"`
Email string `json: "email" bson: "email"`
Gender string `json: "gender" bson: "gender"`
Password string `json: "password" bson: "password"`
Enabled bool `json: "enabled" bson: "enabled"`
BirthDate time.Time `json: "birthDate" bson: "birthDate"`
CreatedAt time.Time `json: "createdAt" bson: "createdAt"`
UpdatedAt time.Time `json: "updatedAt" bson: "updatedAt"`
collection *mongo.Collection
}
Run Code Online (Sandbox Code Playgroud)
然后使用查询数据
func (u *User) FindByUsername(userName string) error {
var ctx, _ = …Run Code Online (Sandbox Code Playgroud) 我正在尝试从数据库中读取数据,然后将结果作为 json 返回给用户。
发生的事情是我得到的输出如下:
[{"Key":"foo","Value":"bar"}]
当我想得到:
"{"foo":"bar"}"
我如何获得前者?
Raw示例:(未显示从数据库读取并转换为字符串)
package main
import (
"encoding/json"
"fmt"
"go.mongodb.org/mongo-driver/bson"
)
func main() {
var data = "{\"foo\":\"bar\"}"
var testInterface interface{}
e := bson.UnmarshalExtJSON([]byte(data), false, &testInterface)
if e != nil {
fmt.Println("err is ", e)
}
out, _ := json.Marshal(testInterface)
fmt.Println(string(out))
// prints: [{"Key":"foo","Value":"bar"}]
}
Run Code Online (Sandbox Code Playgroud) 以下代码尝试通过将新文档插入到mongoDB中 go.mongodb.org/mongo-driver
data := "this is test string blablablablablablabla"
type Doc struct {
Version int "json:version, bson:version"
Data string "json:data, bson:data"
}
dd := Doc{Version: 21, Data: data}
dObj, _ := json.Marshal(dd)
queryFilter := bson.M{"version": 1}
update1 := bson.M{"$set": bson.M{"version": 1, "data": json.RawMessage(dObj)}}
// insert data with json.RawMessage
_, err := db.Mongo("test").Collection("test_doc1").UpdateOne(context.Background(), queryFilter, update1, options.Update().SetUpsert(true))
if err != nil {
fmt.Println("failed to insert doc1")
}
update2 := bson.M{"$set": bson.M{"version": 1, "data": (dObj)}}
// insert data without json.RawMessage
_, err = …Run Code Online (Sandbox Code Playgroud)