mgo API – mgo、mgo/bson、mgo/txn 之间的区别是什么?

huangapple go评论109阅读模式
英文:

mgo API - difference between mgo, mgo/bson, mgo/txn

问题

最近我开始学习Go语言,并且目前非常喜欢它。我想学习如何使用mgo Mongo驱动程序创建一个REST API。

在网站上,有三个API:mgomgo/bsonmgo/txn。它们代表什么意思?

英文:

I recently started learning Go and really like it so far. I would like to learn how to make a REST API with the mgo Mongo driver.

At the website, there are three APIs: mgo, mgo/bson, mgo/txn. What do they mean?

答案1

得分: 4

Darshan是正确的:在项目网站上有关于这些细节的好参考资料。具体来说,你可以在mgomgo/bsonmgo/txn的包API链接中找到相关信息。

以下是对每个包的快速概述,因为网页上似乎缺少这部分(我应该修复一下):

labix.org/v2/mgo

这是MongoDB驱动程序本身。如果你想与MongoDB数据库交互,这是你应该开始使用的包。可以查看mgo.Dial函数和项目网站中的示例。

labix.org/v2/mgo/bson

该包实现了BSON文档的编组和解组,遵循BSON规范。它不依赖于其他两个包,当你想要以任何原因简单地序列化/反序列化该格式的文档时,可以单独使用它。

mgo包使用它来实现所有的编组和解组功能,因此在使用mgo包时,你在mgo/bson包的文档中找到的关于字段标签等细节都是有效的。

labix.org/v2/mgo/txn

该包实现了mgo特定的MongoDB多文档事务支持。它在mgo包的基础上实现其功能,其他两个包都不依赖于它。如果你刚开始使用MongoDB,很可能不需要这个包。

英文:

Darshan is right: there are good references for those details at the project website. Specifically, you can find links to the package APIs of mgo, mgo/bson, and mgo/txn

Just providing a quick overview of each of the packages, since this seems missing from the web page (I should fix that):

labix.org/v2/mgo

This is the MongoDB driver itself. If you want to talk to a MongoDB database, this is the package to start with. Have a look at the mgo.Dial function, and the example in the project website.

labix.org/v2/mgo/bson

This package implements the marshaling and unmarshaling of BSON documents, following the BSON specification. It doesn't depend on any of the other two packages, and may be used by itself when one wants to simply serialize/deserialize documents in that format for whatever reason.

The mgo package uses it to implement all marshaling and unmarshaling functionality, so the details you find in the documentation of the mgo/bson package in terms of field tags, etc, are all valid when working with the mgo package as well.

labix.org/v2/mgo/txn

This package implements the mgo-specific multi-document transaction support for MongoDB. It implements its functionality on top of the mgo package, and neither of the other two packages depends on it. If you're just getting started with your MongoDB use, you most probably don't need this package.

答案2

得分: 1

它们不是三个独立的API,而是一个模块化的API。mgo是核心包,mgo/bson是BSON实现,mgo/txn支持多文档事务。

你可以先使用mgo,如果需要它们提供的功能,再添加其中的任何一个子包。在mgo首页上有示例代码展示了如何使用。

英文:

They aren't three separate APIs; it's one API that's modularized. mgo is the core package. mgo/bson is the BSON implementation. mgo/txn supports multi-document transactions.

You'd start by just using mgo and add either of the subpackages if you need the features they provide. There's example code at the mgo homepage that demonstrates usage.

huangapple
  • 本文由 发表于 2013年9月8日 11:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/18680093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定