mgo

Enforce a type mapping with mgo

懵懂的女人 提交于 2019-12-13 16:54:01
问题 An _id member is not mapped to type ObjectId anymore, when its type is only derived from bson.ObjectId: import ( "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type CustomId bson.ObjectId type Foo struct { ID1 CustomId `bson:"_id"` // broken ID2 bson.ObjectId // mapped as expected } func main() { session, _ := mgo.Dial("127.0.0.1") coll := session.DB("mgodemo").C("foocoll") doc := Foo{ CustomId(bson.NewObjectId()), bson.NewObjectId(), } coll.Insert(doc) } The _id should have been an ObjectId in

Query where sum of two fields is less than given value

江枫思渺然 提交于 2019-12-13 10:18:34
问题 I am using Go language and MongoDB with mgo.v2 driver and I have struct like type MarkModel struct { ID bson.ObjectId `json: "_id,omitempty" bson: "_id,omitempty"` Name string `json: "name" bson: "name"` Sum int `json: "sum" bson: "sum"` Delta int `json: "delta" bson: "delta"` } I need to find all where is Sum + Delta < 1000 for example. At the moment I load all and then in Go code I filter but I would like to filter on query level. How to make that query ? At the moment I return all with

Search array of nested objects by given field

会有一股神秘感。 提交于 2019-12-13 07:18:33
问题 I have the following structure of the Room object. type Room struct { Id bson.ObjectId `json:"id" bson:"_id,omitempty"` Title string `json:"title" bson:"title"` Description string `json:"description" bson:"description,omitempty"` Type string `json:"type" bson:"type,omitempty"` AdminId bson.ObjectId `json:"admin_id" bson:"admin_id"` CreatedOn time.Time `json:"created_on" bson:"created_on"` Messages []Message `json:"messages" bson:"messages,omitempty"`} Where Messages is nested array of objects

mgo $unwind aggregation result to Unknown element kind (0x2E)

给你一囗甜甜゛ 提交于 2019-12-13 02:17:02
问题 I have an aggregate query like this $ db.histories.aggregate([{$match:{"issue_id":{$in:ids},"history_comment":{$exists:true,$not:{$size:0}}}},{$unwind:"$history_comment"}]) translating this to go using mgo var h []History query := []bson.M{ {"$match": bson.M{ "issue_id": bson.M{"$in": IDs}, "history_comment": bson.M{"$exists": true, "$not": bson.M{"$size": 0}}}}, {"$unwind": "$history_comment"}, } err := c.Pipe(query).All(&h) but I received an err Unknown element kind (0x2E) how is this

MongoDB slice query into golang

◇◆丶佛笑我妖孽 提交于 2019-12-12 20:04:00
问题 How can i write this below slice query into golang? db.con.find({"repoid":1356485},{"contr":{$slice:[0,10]}}).pretty() Tried with this but not working DB.C("con").Find(bson.M{"id": ID, "contr": bson.M{"$slice": []interface{}{"$contr", offset, limit}}}) does not find anything. Any ideas? Thank you in advance 回答1: With Collection.Find() you can only specify the filter. But what you have is a projection: {"contr":{$slice:[0,10]} Projections can be specified using Query.Select(), so this is how

Golang mongodb aggregation

时间秒杀一切 提交于 2019-12-12 19:17:29
问题 I have a collection of users. User have an int64 "id", and lets say "avatar", "name" and an array of other users ids. What I want to achieve is to query SINGLE user, but instead of getting an array with his friends ids I want to get an array of his friends, containing their names and avatars. How to achieve it in golang? I have found some kind of what I need - "lookup" function, but I cannot understand how to use it right. 回答1: You cannot apply $lookup to array directly, but you can to

How should I handle UUID fields using mgo?

谁都会走 提交于 2019-12-12 12:06:04
问题 I have this Document in MongoDB: { "_id": { "$oid": "5ad0873b169ade0001345d34" }, "j": { "$uuid": "94482b86-1005-e3a0-5235-55fb7c1d648a" }, "v": "sign", "d": "a", "s": "init", "response": {}, "creation_date": { "$date": "2018-04-13T10:32:27.140Z" } } I want to filter & fetch some documents in Golang using mgo, and here's my code: package main import ( "fmt" "log" "time" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type JOB struct { ID bson.ObjectId `bson:"_id,omitempty"` Key string `bson:"j"`

MongoDB in Go (golang) with mgo: how to use logical operators to query?

左心房为你撑大大i 提交于 2019-12-12 08:24:21
问题 I would like to run the following query in golang using mgo in a pipeline. {"key1" : 1, "$or" : [{"key2" : 2}, {"key3" : 2}]} I have looked everywhere, but I cannot find an example like this. I have tried many different combinations, for example: ... pipeline := []bson.M{ bson.M{ "$match" : bson.M{ "key1" : 1, "$or" : bson.M{ "key2" : 2, "key3" : 2}, } ... } which compiles correctly, does not find anything. Any ideas? Thank you in advance 回答1: Your mongo query can be translated to the

mgo NewObjectId corrupt on insert

夙愿已清 提交于 2019-12-12 06:22:34
问题 If I generate a new object id for a document in mgo: obId := bson.NewObjectId() and then insert it, it ends up in mongo (looking via the cli) as "_id" : "U�`�\u0006@�\rU\u0000\u0000\u0001" When it should be "_id" : ObjectId("559a47643d9827f0d9405420") Same goes if I try and update an existing document where I generate the id by obId := bson.ObjectIdHex(stringId) It still gets serialized to the corrupted format. My struct which I'm trying to insert looks like this: type MyStruct struct { Id

Go nested object with mgo adapter

筅森魡賤 提交于 2019-12-12 03:24:01
问题 I'm working on a project that based on MongoDB data structure. Our objects that stored inside the database looks like this: { "_id" : ObjectId("567a877df1c7720bea7c2f51"), "username" : "dog", "type" : "regular", "data" : { "full" : { "xx" : "xx", "xx" : "xx", "yy" : { "xx" : "test" }, "yy" : { "xx" : { } } } } And the struct that we working on with Golang looks like this: type User struct { Id bson.ObjectId `bson:"_id,omitempty"` username string Type string data struct { full struct { xx