我们如何使用MongoDB的Golang驱动程序创建时间序列集合?

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

How do we create timeseries collections with mongodb golang driver?

问题

我正在尝试创建时间序列集合,使用以下函数:https://www.mongodb.com/developer/how-to/new-time-series-collections/#expireafterseconds

如下所示,我尝试了不同的迭代来将选项传递给CreateCollection()函数,但是没有任何效果。我搜索了几个小时,找不到任何示例,人们可能会认为根据当前的文档很容易设置,但几个小时过去了,我还是需要帮助。

以下是你提供的代码:

func CollectionsTimeSeries(name string) {

	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)

	options := { 
		"timeseries": {
			"timeField": "time_stamp",
			"metaField": "stock",
			"granularity": "minutes",
		},
		"expireAfterSeconds": 2592000 		
	}

	database.GetConnection.CreateCollection(ctx, name, options)
}

你得到了以下错误:

syntax error: unexpected {, expecting expression
syntax error: unexpected }, expecting comma or )
英文:

I am trying to create timeseries collections => https://www.mongodb.com/developer/how-to/new-time-series-collections/#expireafterseconds with the following function
As you can see below i have tried different iterations to pass options to the CreateCollection() but nothing works. Searched for hours for an example of this and could not find any, and one would think will be easy to setup based on current documentation but hours later and here to get help

func CollectionsTimeSeries(name string) {

	ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)

	// options := {
	// 	"TimeSeriesOptions": {
	// 		"TimeField": "time_stamp",
	// 		"MetaField": "stock",
	// 		"Granularity": "minutes",
	// 	},
	// 	"ExpireAfterSeconds": 2592000, 		
	// }

	// options := { 
	// 	timeseries: {
	// 		timeField: "time_stamp",
	// 		metaField: "stock",
	// 		granularity: "minutes",
	// 	},
	// 	expireAfterSeconds: 2592000 		
	// }

    options := { 
    "timeseries": {
        "timeField": "time_stamp",
        "metaField": "stock",
        "granularity": "minutes",
    },
    "expireAfterSeconds": 2592000 		
    }

	// database.GetConnection.CreateCollection(ctx, name, { 
	// 	timeseries: {
	// 		timeField: "time_stamp",
	// 		metaField: "stock",
	// 		granularity: "minutes",
	// 	},
	// 	expireAfterSeconds: 2592000 		
	// })


    database.GetConnection.CreateCollection(ctx, name, options)
}

Get the following errors

syntax error: unexpected {, expecting expression
syntax error: unexpected }, expecting comma or )

答案1

得分: 2

使用options包创建一个包含时间序列选项的选项实例:

opt := options.CreateCollection().
   SetTimeseriesOptions(options.TimeSeries().
        SetGranularity("...").
        SetMetaField("...").
        SetTimeField("..."))
英文:

Use the options package to create an options instance containing time series options:

opt:=options.CreateCollection().
   SetTimeseriesOptions(options.TimeSeries().
        SetGranularity("...").
        SetMetaField("...").
        SetTimeField("..."))

huangapple
  • 本文由 发表于 2022年2月1日 07:17:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/70933972.html
匿名

发表评论

匿名网友

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

确定