Using composite _id in mongo _id:{sid:1,text:"SID"} . bson.M of this is not matching some time

61 views
Skip to first unread message

Siddharth Jain

unread,
Feb 5, 2015, 12:13:34 AM2/5/15
to mgo-...@googlegroups.com, Yadvendar Champawat
Hi

I am using composite _id in mongo

While trying to search same result sometimes it fails to provide result as i am using bson.M which is unordered.

comp_id := bson.M{TBConstants.SID: sid, "text": "SID"}
  
    count, err := c.Find(bson.M{ "_id": comp_id}).Count()


Using bson.D wont give me  bson for search.

and  we have to search  to use the indexing provided  by mongo
db.test.find({_id:{sid:1,text:"SID"}})

and bson.D gives {{_id.sid:1},{_id.text:"SID"}}

Does any  other solution exists

Thanks
Sid

Gustavo Niemeyer

unread,
Feb 9, 2015, 11:38:40 AM2/9/15
to mgo-...@googlegroups.com, Siddharth Jain, Yadvendar Champawat
Hi Siddharth,

The bug there is that Go maps do not have a defined order, so your
identifier document will sometimes have (A, B) keys, and sometimes it
will have (B, A) keys in the generated bson document. To fix that, use
bson.D or a struct when handling such keys (recommend the latter).
> --
> 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.



--

gustavo @ http://niemeyer.net

Siddharth Jain

unread,
Feb 9, 2015, 10:51:55 PM2/9/15
to Gustavo Niemeyer, mgo-...@googlegroups.com, Yadvendar Champawat
Thanks Gustavo..

Currently using bson.D

Why we should prefer struct is there any specific  reason for preference.

Thanks
Sid

Gustavo Niemeyer

unread,
Feb 10, 2015, 3:22:19 AM2/10/15
to Siddharth Jain, Yadvendar Champawat, mgo-...@googlegroups.com

They are much more convenient in general, are safer to use due to static typing, and are a bit more efficient in terms of memory use.

sundar.v...@gmail.com

unread,
Mar 1, 2015, 12:36:52 AM3/1/15
to mgo-...@googlegroups.com, s...@testbook.com, yadv...@testbook.com
Hi Gustavo,

With structures, would it be possible to also do a comparative query, for e.g

If I want all users with age greater than ($gt) 10.

Thanks,
Sundar.
Reply all
Reply to author
Forward
0 new messages