关于使用Mongo Go驱动程序的CRUD日志的问题

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

Question about the crud logs with mongo go driver

问题

我有一个简单的CRUD book应用程序,使用golangmongo-go-driver

需求:将每个CRUD操作记录到一个集合中的日志。

例如:

  1. 创建一本书
  2. 将书插入到book集合中
  3. book.created事件插入到history集合中,包括bookIdtimestamp等信息

问题:

  1. 想知道实现这个功能的推荐方法是什么?
  2. mongo-go-driver中是否有hook功能?(在集合的更新事件等触发时会触发hook
英文:

I have a simple CRUD book app with,
golang, mongo-go-driver

Want: logs every CRUD actions to a single collection.

e.g:

  1. create a book
  2. insert the book to book collection
  3. insert book.created event to history collection with bookId, timestamp..etc

Questions:

  1. Wondering what is the recommended way to implement this feature?
  2. Is there a hook feature in mongo-go-driver? (hook will be triggered on update event on a collection..etc)

答案1

得分: 2

我对Go本身不太熟悉,但一般在MongoDB(客户端)中,处理这个问题的方法是使用命令监控,我认为Go驱动程序也支持这种方法。你可以在这里和这里阅读相关信息。你需要的事件是CommandSucceededEvent

英文:

I'm not familiar with Go itself, but the general mongo (client-side) approach for this is command monitoring which I assume go driver also supports. You can read something about it here and here. The event you need is CommandSucceededEvent

答案2

得分: 1

这目前不支持,如果你考虑一下,插入和更新可能不会在驱动程序级别发生,而是在数据库本身中发生(例如,聚合结果可能会“转储”到集合中)。

相反,你可以查看MongoDB数据库本身支持的change streams。你可以“订阅”变更事件,当发生变更操作时,你将收到通知。

这与你所要求的不完全相同,因为变更的源(或原因)可能不是来自你的应用程序,所以这可能对你来说有利也可能没有优势。

你可以使用Collection.Watch()方法订阅集合的变更。以下是文档中的示例,说明如何订阅并接收“插入”操作的通知:

var collection *mongo.Collection

// 指定一个只匹配“插入”事件的管道。
// 设置MaxAwaitTimeOption以使每次尝试等待两秒钟以获取新文档。
matchStage := bson.D{{"$match", bson.D{{"operationType", "insert"}}}}
opts := options.ChangeStream().SetMaxAwaitTime(2 * time.Second)
changeStream, err := collection.Watch(
	context.TODO(),
	mongo.Pipeline{matchStage},
	opts)
if err != nil {
	log.Fatal(err)
}

// 按接收顺序打印所有变更流事件。
// 有关使用变更流的更多示例,请参阅mongo.ChangeStream文档。
for changeStream.Next(context.TODO()) {
	fmt.Println(changeStream.Current)
}

你可以从MongoDB文档中订阅的操作列表包括:

  • insert

  • delete

  • replace

  • update

  • drop

  • rename

  • dropDatabase

  • invalidate

英文:

This is currently not supported, and if you think about it, insertion and update may not happen at the driver level, but in the database itself (e.g. an aggregation result may be "dumped" into a collection).

Instead you may take a look at change streams supported by the MongoDB database itself. You may "subscribe" to change events, and when a change operation happens, you'll get notified.

This is not exactly the same what asked, because the source (or cause) of change may not come from your application, so this may or may not be an advantage to you.

You may subscribe to changes of a collection using the Collection.Watch() method. Here's an example from the documentation how to subscribe and get notified of "insert" operations:

var collection *mongo.Collection

// Specify a pipeline that will only match "insert" events.
// Specify the MaxAwaitTimeOption to have each attempt wait two seconds for
// new documents.
matchStage := bson.D{{"$match", bson.D{{"operationType", "insert"}}}}
opts := options.ChangeStream().SetMaxAwaitTime(2 * time.Second)
changeStream, err := collection.Watch(
	context.TODO(),
	mongo.Pipeline{matchStage},
	opts)
if err != nil {
	log.Fatal(err)
}

// Print out all change stream events in the order they're received.
// See the mongo.ChangeStream documentation for more examples of using
// change streams.
for changeStream.Next(context.TODO()) {
	fmt.Println(changeStream.Current)
}

The list of supported operations you may subscribe from the MongoDB docs:

  • insert
  • delete
  • replace
  • update
  • drop
  • rename
  • dropDatabase
  • invalidate

huangapple
  • 本文由 发表于 2021年11月9日 20:43:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/69898352.html
匿名

发表评论

匿名网友

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

确定