dp610807 2014-08-09 11:26
浏览 428
已采纳

将MongoDB查询发送到其他系统:转换为JSON,然后解码为BSON? 用Go语言怎么做?

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 }}}
  • 写回答

1条回答

  • douken0530 2014-08-11 19:15
    关注

    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
    ", 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
    ", m)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作