英文:
Question about the crud logs with mongo go driver
问题
我有一个简单的CRUD book
应用程序,使用golang
和mongo-go-driver
。
需求:将每个CRUD操作记录到一个集合中的日志。
例如:
- 创建一本书
- 将书插入到
book
集合中 - 将
book.created
事件插入到history
集合中,包括bookId
、timestamp
等信息
问题:
- 想知道实现这个功能的推荐方法是什么?
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:
- create a book
- insert the book to
book
collection - insert
book.created
event tohistory
collection withbookId
,timestamp
..etc
Questions:
- Wondering what is the recommended way to implement this feature?
- Is there a
hook
feature inmongo-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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论