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#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。