mgo

Obtain ObjectIdHex value from mgo query

余生颓废 提交于 2019-12-10 15:18:30
问题 I'm still new to go and while I see multiple questions on SO similar to this, I'm unable to reproduce the output some OP's had requested (this answer looking the closest). I'm doing something fairly simple, I'm hitting a users collection in mongo and all I want to do is get the _id value back as a string. I'm going to eventually push these _id 's up to NSQ but that's the brunt of my task. var users []bson.M err = sess.DB("db_name").C("users").Find(bson.M{}).All(&users) if err != nil { os.Exit

golang mongodb (mgo) is not inserting docs

给你一囗甜甜゛ 提交于 2019-12-10 10:58:25
问题 Im having issues with persisting a golang struct in mongodb using mgo. type AN_Track_Log struct { Id bson.ObjectId `bson:"_id,omitempty"` user_session_id_str string `bson:"user_session_id_str"` googleanaly_pixel_id_str string `bson:"googleanaly_pixel_id_str"` perfaud_pixel_id_str string `bson:"perfaud_pixel_id_str"` site_id_str string `bson:"site_id_str"` metric_str string `bson:"metric_str"` value_str string `bson:"value_str"` event_str string `bson:"event_str"` location_id_str string `bson:

Concurrent queries to MongoDB using mgo ends in closed sockets

て烟熏妆下的殇ゞ 提交于 2019-12-10 09:44:41
问题 I have been checking several questions here in stackoverflow + some posts where people exposes examples of of how to manage sessions with mgo golang lib . The point is that all the examples I've seen doesn't run too much concurrent queries at the same time. Increasing the number of concurrent operations ends in closed sockets. Here you can find the code I ran in order to reproduce this behaviour. Concurrent queries to MongoDB using mgo ends in closed sockets. Note that I'm just running 200 of

Parsing error in mongodb db, insert to collection with unique index

随声附和 提交于 2019-12-10 08:50:46
问题 I have a collection in mongodb with documents of the form: { "user": "user1", "email: "user1@example.com", } Where the fields "user" and "email" are unique. I want to insert a new user to the collection, while checking for uniqueness of both values. I can do an insert in golang with mgo like this: session.SetSafe(&mgo.Safe{}) // ensure mgo waits for errors user := struct{ string `bson:"user"` string `bson:"email"` }{ "user1", "user1@different.com" } err := users.Insert(user) // where user is

How do I create a text index in mongodb with golang and the mgo library?

非 Y 不嫁゛ 提交于 2019-12-10 03:31:48
问题 I'm trying to do a full text search on a collection, but in order to do that I need to create a text index (http://docs.mongodb.org/manual/tutorial/create-text-index-on-multiple-fields/) The mgo library provides an EnsureIndex() function however, it only accepts a slice of strings as a key. I tried just writing the index out as a string: { name: "text", about: "text" } and passing it to that function but it didn't work. I've also managed to manually create the index in the mongo shell but I'd

How to marshal json string to bson document for writing to MongoDB?

时光总嘲笑我的痴心妄想 提交于 2019-12-09 11:10:33
问题 What I am looking is equivalent of Document.parse() in golang, that allows me create bson from json directly? I do not want to create intermediate Go structs for marshaling 回答1: The gopkg.in/mgo.v2/bson package has a function called UnmarshalJSON which does exactly what you want. The data parameter should hold you JSON string as []byte value. func UnmarshalJSON(data []byte, value interface{}) error UnmarshalJSON unmarshals a JSON value that may hold non-standard syntax as defined in BSON's

How can I find nearby place with latitude and longitude in mongodb?

被刻印的时光 ゝ 提交于 2019-12-09 07:00:21
问题 Am very new to mongodb and golang. I have a collection named "myplace" It has the following fileds place_name, city, latitude, longitude. My question is user in some place and searching the nearby places. How can I query to mongodb to find near by locations. Also in golang. My doc structure { "_id" : ObjectId("544a2147785b707b340ed6c7"), "latitude" : 12.36547, "longitude" : 1.235689, "place_name" : "some_place", "city" : "Some city" } Thanks in advance 回答1: Hi For your case I think you should

mgo time.Time or boolean check

只谈情不闲聊 提交于 2019-12-08 08:00:29
I have a mongo document which contains a date field which can also be false (or not defined), and I can't seem to find how to check if the field is available OR is false OR is a date (time.Time) in golang/mgo :S If you have a time.Time field, and want to know whether it was properly set with a valid date, you can query its IsZero() method. Otherwise, if you're trying to query the database for such a document, you can do one of the following. Query if the field is false: iter := collection.Find(bson.M{"field": false}).Iter() Query if the field is available, with the $exists operator : iter :=

mgo - bson.ObjectId vs string id

大憨熊 提交于 2019-12-08 06:56:46
问题 Using mgo , it seems that best practice is to set object ids to be bson.ObjectId . This is not very convenient, as the result is that instead of a plain string id the id is stored as binary in the DB. Googling this seems to yield tons of questions like "how do I get a string out of the bson id?", and indeed in golang there is the Hex() method of the ObjectId to allow you to get the string. The bson becomes even more annoying to work with when exporting data from mongo to another DB platform

Only return the looked up document with Mongo and Golang

独自空忆成欢 提交于 2019-12-08 06:24:10
问题 I have these two models : // EventBoost describes the model of a EventBoost type EventBoost struct { ID string `bson:"_id" json:"_id" valid:"alphanum,printableascii"` Name string `bson:"name" json:"name"` Description string `bson:"description" json:"description"` Level string `bson:"level" json:"level"` EventID string `bson:"_event_id" json:"_event_id" valid:"alphanum,printableascii"` StartDate time.Time `bson:"start_date" json:"start_date"` EndDate time.Time `bson:"end_date" json:"end_date"`