本文介绍了发送一个MongoDB查询到不同的系统:转换成JSON然后解码成BSON?如何在Go语言中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将MongoDB查询传送到不同的系统。出于这个原因,我想使用。我需要这样做主要是因为我在查询中使用了日期比较。

因此,问题的核心是我需要传递一个MongoDB查询在 node.js 后端生成到以 Go 语言编写的另一个后端。

直观上, JSON是通过 REST 发送此查询的最明显格式。但是,MongoDB查询并不完全是JSON,而是BSON,它包含日期的特殊构造。



所以,想法是将查询转换为JSON,使用作为特殊结构的表示形式。经过一些测试后,很明显这些查询不起作用。通过 node.js 发送的MongoDB shell和查询都需要特殊的 ISODate new Date 结构。



最后,实际的问题是:是否有函数将JSON编码/解码为BSON,并考虑到,都使用JavaScript(node.js)和 Go 语言。

更新



Node.js编码包



显然,解析和串化BSON / JSON。
所以,我的问题的一半已解决。我想知道在 Go 语言中是否有这样的内容。


示例查询



例如,以下查询位于普通BSON中:

  {Tmin:{$ gt:ISODate(2006-01-01T23:00:00.000Z)}} 
MongoDB Extended JSON ,它变成:

<$> p $ p> {Tmin:{$ gt:{$ date:1136156400000}}}


解决方案

经过一番研究,我发现了图书馆,但它仅用于编组,所以我决定编写一个Unmarshaller。



注意(我写到了),现在它是一个非常简单的 ejson - > bson converter,没有 bson - > ejson 但是,您可以使用 mejson



An :



 常量J =`{ _id:{ $ OID: 53c2ab5e4291b17b666d742a}, last_seen_at:{$日期 :1405266782008}, DISPLAY_NAME :{ $未定义 :真正} 
裁判:{ $ REF : COL2\" , $标识: 53c2ab5e4291b17b666d742b}}`

型测试结构{
编号bson.ObjectId`BSON: _ ID`
LastSeenAt *`了time.time BSON: last_seen_at`
显示名称*字符串'BSON :display_name,omitempty`
Ref mgo.DBRef`bson:ref`
}

func main(){
var ts TestS
如果err:= ejson.Unmarshal([] byte(j),& ts); err!= nil {
panic(err)
}
fmt.Printf(%+ v \ n,ts)

//或转换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)

}


I need to transfer a MongoDB query to a different system. For this reason I would like to use the MongoDB Extended JSON. I need this to be done mostly because I use date comparisons in my queries.

So, the kernel of the problem is that I need to transfer a MongoDB query that has been generated in a node.js back-end to another back-end written in Go language.

Intuitively, the most obvious format for sending this query via REST, is JSON. But, MongoDB queries are not exactly JSON, but BSON, which contains special constructs for dates.

So, the idea is to convert the queries into JSON using MongoDB Extended JSON as form of representation of the special constructs. After some tests it's clear that these queries do not work. Both the MongoDB shell and queries sent via node.js's need the special ISODate or new Date constructs.

Finally, the actual question: are there functions to encode/decode from JSON to BSON, taking into account MongoDB Extended JSON, both in JavaScript (node.js) and Go language?

Updates

Node.js encoding package

Apparently there is a node.js package that parses and stringifies BSON/JSON.So, half of my problem is resolved. I wonder if there is something like this in Go language.

Sample query

For example, the following query is in normal BSON:

{ Tmin: { $gt: ISODate("2006-01-01T23:00:00.000Z") } }

Translated into MongoDB Extended JSON, it becomes:

{ "Tmin": { "$gt" : { "$date" : 1136156400000 }}}
解决方案

After some research I found the mejson library, however it's for Marshaling only, so I decided to write an Unmarshaller.

Behold ejson (I wrote it), right now it's a very simple ejson -> bson converter, there's no bson -> ejson yet, you can use mejson for that.

An 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)

}

这篇关于发送一个MongoDB查询到不同的系统:转换成JSON然后解码成BSON?如何在Go语言中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 12:20