Exact Timestamp query in mongodb with golang

4,783 views
Skip to first unread message

rama moorthy

unread,
Jun 29, 2014, 11:20:21 PM6/29/14
to mgo-...@googlegroups.com
I have to query the collections based on the exact timestamp, how do i do that?

Current Setup :

I have this collection in my mongodb :

{
    "_id" : ObjectId("53ac9c88bb1b8c60b9f3a9d3"),
    "path" : "Code_Snippets/Create/Table/ID115_Date_Loop_By_Rec",
    "name" : "id115_date_loop_by_rec",
    "lastmodifiedtime" : ISODate("2014-06-30T02:00:46.000Z")
}

My golang sample query is 

type ScriptSet struct {
ID               bson.ObjectId `json:"id" bson:"_id,omitempty"`
Path             string        `json:"path"`
Name             string        `json:"name"`
LastModifiedTime time.Time     `json:"lastmodifiedtime"`
}

var ss Scriptset{}
ss.Path="/usr/sample/folder/path"
ss.LastModifiedTime= lastModifiedTime(ss.Path) 

func lastModifiedTime(path string) time.Time {
info, _ := os.Stat(path)

return info.ModTime()
}

the final query is 
var s interface{}

find({"path": ss.Path, "LastModifiedTime": ss.LastModifiedTime}).One(&s)

it does not seem to match, it says not found.

please help me 

Thanks.






Klaus Post

unread,
Jun 30, 2014, 9:17:43 AM6/30/14
to mgo-...@googlegroups.com
Hi!

Maybe character case in "LastModifiedTime" is an issue?  The bson encoder uses initial lower case, so that may lead to a mismatch?

May I ask why you need to match a time exactly - isn't there a "safer" way to do this?

Regards,

Klaus

Norberto Leite

unread,
Jun 30, 2014, 9:24:05 AM6/30/14
to mgo-...@googlegroups.com
Klaus is correct,

if you run: db.col.find() you will see that the document field is 'lastmodifiedtime' but your query is {... 'LastModifiedTime':  ISODate("2014-06-30T02:00:46.000Z")} this won't match.


--
You received this message because you are subscribed to the Google Groups "mgo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mgo-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--


{ "name"     : "Norberto Leite",

  "title"    : "SA | Eng",

  "phone"    : "0034.687.954.987",

  "location" : ["Barcelona, ES", "London, UK"],

  "twitter"  : ["@MongoDB""@MongoDBInc", "@nleite"],

  "facebook" : ["MongoDB", "MongoDB, Inc."] }

rama moorthy

unread,
Jul 6, 2014, 9:52:58 PM7/6/14
to mgo-...@googlegroups.com
Hi Guys

Thanks for all your help. yes you guys are right, exact timestamp match is not safer, it did not go through reviewal of code. even through i got it done.

i resolved this issue with the following struct

type findQuery struct {

path string `json:"path"`
timelastmodifiedtime time.Time `json:"lastmodfiedtime"`

}

var fq findQuery

fq.path = "sample path"
fq.lastmodifiedtime = time.Time

i send this to mongo driver, it actually works. but its not safer.

i have changed it to bson.M using "$gt". i will post the exact code later

Thanks everyone

rama moorthy

unread,
Jul 7, 2014, 2:02:55 AM7/7/14
to mgo-...@googlegroups.com
This is the actual code that i am using


bson.M{"path": ss.Path, "lastmodifiedtime": bson.M{"$gte": ss.LastModifiedTime}}

Thanks guys


On Monday, June 30, 2014 11:20:21 AM UTC+8, rama moorthy wrote:
Reply all
Reply to author
Forward
0 new messages