在 mongodb 记录中使用 time.Time
Using time.Time in mongodb record
我正在 collection 中插入新项目。为此使用官方 mongo go 驱动程序 (https://github.com/mongodb/mongo-go-driver)。
collection.InsertOne(context.Background(), map[string]interface{}{
"string": "test",
"integer": 123,
"float": 0.123,
"array": []string{"t", "e", "s", "t"},
"objectid": objectid.New(),
"time": time.Now(),
})
但结果我遇到了几个属性的问题:time.Time 和 objectid.ObjectID。
- time.Time 变为 Object 为空
- objectid.ObjectID - 作为二进制
我知道它只是处于 alpha 状态,但也许有人知道。是我做错了还是还没有按照应有的方式实施?
您需要将时间转换为字符串或 unix 时间戳。
time.Now()
是一个时间类型,它是一个结构。
a := time.Now()
fmt.Println(a.Unix()) // Goes into mongodb as a int64
fmt.Println(a.String()) // Goes inot mongodb as a string
1522868253
// Unix
2018-04-04 13:57:33.495965 -0500 CDT m=+0.000363419
// 字符串
所以你可以这样做
collection.InsertOne(context.Background(), map[string]interface{}{
"string": "test",
"integer": 123,
"float": 0.123,
"array": []string{"t", "e", "s", "t"},
"objectid": objectid.New(),
"time": time.Now().String(),
})
如果你传递一个map
作为文档给Collection.InsertOne()
, the mongo
package will use the mongo.TransformDocument()
to convert it to a *bson.Document
值,因为大部分操作只在bson.Document
s上实现。
当前的转换实现不处理 objectid.ObjectID
nor the time.Time
类型。它可以而且可能应该,我认为它会,但目前它没有。
如果您希望这些类型以 MongoDB 中的正确类型结束,您可以自己构造并传递一个 *bson.Document
,您可以在其中明确指定属性的类型应该是什么.
这是与您的等效的插入语句,使用 bson.NewDocument()
手动创建文档:
res, err := coll.InsertOne(context.Background(), bson.NewDocument(
bson.EC.String("string", "test"),
bson.EC.Int64("integer", 123),
bson.EC.Double("float", 0.123),
bson.EC.ArrayFromElements("array",
bson.VC.String("t"), bson.VC.String("e"),
bson.VC.String("s"), bson.VC.String("t")),
bson.EC.ObjectID("objectid", objectid.New()),
bson.EC.DateTime("time", time.Now().UnixNano()/1e6), // Must pass milliseconds
))
它更冗长,但它在我们希望 MongoDB 中的结果文档中是明确的。结果文档将如下所示:
{
"_id" : ObjectId("5ac5f598ca151255c6fc0ffb"),
"string" : "test",
"integer" : NumberLong(123),
"float" : 0.123,
"array" : [
"t",
"e",
"s",
"t"
],
"objectid" : ObjectId("5ac5f598ca151255c6fc0ffa"),
"time" : ISODate("2018-04-05T10:08:24.148Z")
}
一旦驱动程序得到改进,我假设您的原始版本将按预期工作并生成与此结构相同的文档。
我正在 collection 中插入新项目。为此使用官方 mongo go 驱动程序 (https://github.com/mongodb/mongo-go-driver)。
collection.InsertOne(context.Background(), map[string]interface{}{
"string": "test",
"integer": 123,
"float": 0.123,
"array": []string{"t", "e", "s", "t"},
"objectid": objectid.New(),
"time": time.Now(),
})
但结果我遇到了几个属性的问题:time.Time 和 objectid.ObjectID。
- time.Time 变为 Object 为空
- objectid.ObjectID - 作为二进制
我知道它只是处于 alpha 状态,但也许有人知道。是我做错了还是还没有按照应有的方式实施?
您需要将时间转换为字符串或 unix 时间戳。
time.Now()
是一个时间类型,它是一个结构。
a := time.Now()
fmt.Println(a.Unix()) // Goes into mongodb as a int64
fmt.Println(a.String()) // Goes inot mongodb as a string
1522868253
// Unix
2018-04-04 13:57:33.495965 -0500 CDT m=+0.000363419
// 字符串
所以你可以这样做
collection.InsertOne(context.Background(), map[string]interface{}{
"string": "test",
"integer": 123,
"float": 0.123,
"array": []string{"t", "e", "s", "t"},
"objectid": objectid.New(),
"time": time.Now().String(),
})
如果你传递一个map
作为文档给Collection.InsertOne()
, the mongo
package will use the mongo.TransformDocument()
to convert it to a *bson.Document
值,因为大部分操作只在bson.Document
s上实现。
当前的转换实现不处理 objectid.ObjectID
nor the time.Time
类型。它可以而且可能应该,我认为它会,但目前它没有。
如果您希望这些类型以 MongoDB 中的正确类型结束,您可以自己构造并传递一个 *bson.Document
,您可以在其中明确指定属性的类型应该是什么.
这是与您的等效的插入语句,使用 bson.NewDocument()
手动创建文档:
res, err := coll.InsertOne(context.Background(), bson.NewDocument(
bson.EC.String("string", "test"),
bson.EC.Int64("integer", 123),
bson.EC.Double("float", 0.123),
bson.EC.ArrayFromElements("array",
bson.VC.String("t"), bson.VC.String("e"),
bson.VC.String("s"), bson.VC.String("t")),
bson.EC.ObjectID("objectid", objectid.New()),
bson.EC.DateTime("time", time.Now().UnixNano()/1e6), // Must pass milliseconds
))
它更冗长,但它在我们希望 MongoDB 中的结果文档中是明确的。结果文档将如下所示:
{
"_id" : ObjectId("5ac5f598ca151255c6fc0ffb"),
"string" : "test",
"integer" : NumberLong(123),
"float" : 0.123,
"array" : [
"t",
"e",
"s",
"t"
],
"objectid" : ObjectId("5ac5f598ca151255c6fc0ffa"),
"time" : ISODate("2018-04-05T10:08:24.148Z")
}
一旦驱动程序得到改进,我假设您的原始版本将按预期工作并生成与此结构相同的文档。