Golang / mgo:我如何让MongoDB在现场使用当前时间? [英] Golang/mgo: How can I ask MongoDB to use current time in a field?

查看:242
本文介绍了Golang / mgo:我如何让MongoDB在现场使用当前时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类型AppInstance结构{
Id bson.ObjectId_id,omitempty
Url字符串
优先级int
LastSeen字符串
}

我希望LastSeen字段能够保存最后一次与特定应用进行交互的时间。所以,应用程序注册自己设置当前时间(作为一个字符串)。

我希望Mongo在插入时自动将当前时间设置到该字段中,就像MySQL的NOW()函数一样。



我有这个帮助函数:

  func mongoNow()bson.JavaScript {
return bson.JavaScript {Code:
return(new Date())。ISODate('YYYY-MM-DD hh:mm:ss ');}
}

我试过这个:

  c:= mongoSession.DB(myapp)。C(instances)
rand.Seed(time.Now()。 UnixNano())
err:= c.Insert(
struct {Id,Serial,Priority,Url,LastSeen interface {}} {
Id:bson.NewObjectId(),
url:getInformedHost()+:+ getRunningPortString(),
优先级:rand.Int(),
LastSeen:mongoNow()}

checkError(err, 无法在MongoDB服务器上注册,3)



d被存储为脚本而不是评估:

  [_ id] => MongoId对象(
[$ id] => 502d6f984eaead30a134fa10

[id] => MongoId对象(
[$ id] => 502d6f98aa443e0ffd000001

[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode对象(
[code] =>(new Date())。ISODate('YYYY-MM-DD hh:mm:ss')
[scope] => Array(


所以,我认为有问题:



首先,我该如何插入当前时间?第二,我怎样才能得到一些JavaScript评估而不是插入?

p>

对第二个答案的回答可能足以回答第一个问题,但它可能不是。

解决方案

不要将时间存储为字符串。 mgo支持 time.Time ,就像 Date

  type事件结构{
Id bson.ObjectId_id,omitempty
哪个字符串
日期时间。时间
}

插入一个事件

  e:=事件{
其中:第一事件,
日期:时间.Now(),
}
c .Insert(e)


I have this struct that matches the types of a MongoDB collection I'm using:

type AppInstance struct {
    Id bson.ObjectId "_id,omitempty"
    Url string
    Priority int
    LastSeen string
}

I want the LastSeen field to hold the time of the last interaction with that particular app. So, the app registers itself setting current time (as a string).

What I would like is Mongo to dynamically set its own current time into that field when it inserts, just like MySQL's NOW() function would do.

I have this helper function:

func mongoNow() bson.JavaScript {
    return bson.JavaScript{Code: 
         "return (new Date()).ISODate('YYYY-MM-DD hh:mm:ss');"}
}

And I tried this:

c := mongoSession.DB("myapp").C("instances")
rand.Seed(time.Now().UnixNano())
err := c.Insert(
   struct{Id, Serial, Priority, Url, LastSeen interface{}}{ 
      Id: bson.NewObjectId(), 
      Url: getInformedHost() + ":" + getRunningPortString(), 
      Priority: rand.Int(), 
      LastSeen: mongoNow() }
)
checkError(err, "Could not register on MongoDB server.", 3)

the LastSeen field gets stored as a script instead of evaluated:

[_id] => MongoId Object (
    [$id] => 502d6f984eaead30a134fa10
)
[id] => MongoId Object (
    [$id] => 502d6f98aa443e0ffd000001
)
[priority] => 1694546828
[url] => 127.0.0.1:8080
[lastseen] => MongoCode Object (
    [code] => (new Date()).ISODate('YYYY-MM-DD hh:mm:ss')
    [scope] => Array (
    )
)

So, I think there are to questions:

First, how can I insert the current time?

Second, how can I get some javascript evaluated instead of inserted?

The answer to the second one could be enough to answer the first one, but it might as well not be.

解决方案

Don't store time as string. mgo supports time.Time which is like a Date object in Javascript:

type Event struct {
    Id    bson.ObjectId "_id,omitempty"
    Which string
    Date  time.Time
}

Insert an event that happened now:

e := Event{
    Which: "first event",
    Date: time.Now(),
}
c.Insert(e)

这篇关于Golang / mgo:我如何让MongoDB在现场使用当前时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆