mgo

How to write a MongoDB $slice in golang

♀尐吖头ヾ 提交于 2019-12-08 05:42:27
问题 How do I write $slice MongoDB code in golang? Here is my MongoDB code: db.userdetail.aggregate([{"$project":{"user":{"$slice":["$user",2,1]}}}]).pretty() I try to write this code in golang: o1 := bson.M{ "$project":bson.M{"user":bson.M{"$slice":bson.M["$user",2,1,],},}, } It gives me the following err: syntax error: unexpected comma, expecting ] 回答1: bson.M is map[string]interface not slice. And you need []interface for "$slice". o1 := bson.M{"$project": bson.M{"user": bson.M{"$slice": [

How to Get an Aggregate from a MongoDB Collection

我怕爱的太早我们不能终老 提交于 2019-12-08 05:28:29
The following collection contains the ticks for the diverse exchange symbols (in this case BTCUSD and BTCEUR ): { "_id" : ObjectId("5a08d2b956df9b2302759d1a"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-13T00:00:00Z"), "open" : 112, "close" : 91.1, "high" : 130, "low" : 81.12, "volume" : 23866.6770456 } { "_id" : ObjectId("5a08d2b956df9b2302759d1c"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-14T00:00:00Z"), "open" : 91.1, "close" : 90.171, "high" : 109, "low" : 20, "volume" : 16437.2196645 } { "_id" : ObjectId("5a08d2b956df9b2302759d1e"), "symbol" : "BTCEUR", "time" : ISODate("2013-04

How to Get an Aggregate from a MongoDB Collection

▼魔方 西西 提交于 2019-12-08 04:37:52
问题 The following collection contains the ticks for the diverse exchange symbols (in this case BTCUSD and BTCEUR ): { "_id" : ObjectId("5a08d2b956df9b2302759d1a"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-13T00:00:00Z"), "open" : 112, "close" : 91.1, "high" : 130, "low" : 81.12, "volume" : 23866.6770456 } { "_id" : ObjectId("5a08d2b956df9b2302759d1c"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-14T00:00:00Z"), "open" : 91.1, "close" : 90.171, "high" : 109, "low" : 20, "volume" : 16437

Only return the looked up document with Mongo and Golang

旧巷老猫 提交于 2019-12-08 03:18:26
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"` IsPublished bool `bson:"is_published" json:"is_published"` CreatedBy string `bson:"created_by" json:

mgo - bson.ObjectId vs string id

扶醉桌前 提交于 2019-12-07 06:57:25
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 (this is the case when dealing with big data that is collected and you want to merge it with some

Golang “Best practice” to handle connection to database

半城伤御伤魂 提交于 2019-12-07 04:20:40
问题 I have a webservice written in Go and currently I have this Global package I import everywhere which contains the connection to MongoDB (via MGO), but I have to say that this feels very icky to me. What is the best practice of maintaining connections to datasources in Go? I come from a PHP world, hence the Global :S 回答1: There is nothing wrong with importing a package which exports an initialized *mgo.Session object either as a package-level variable or through an accessor/initializer. Maybe

Using golang and mgo, how do I search for a range of values in MongoDB?

孤人 提交于 2019-12-07 02:11:48
问题 I worked through the example on the mgo homepage, but I'm struggling to find a way to query a range of values. The line: searchResults, searchErr = SearchReading(bson.M{"k": key, "t": { $gte: start, $lte: end } }, limit) fails with: line67: syntax error: unexpected $ line67: missing type in composite literal I left out the non-essential bits of code... type Reading struct { K string "k" T int64 "t" V float64 "v" } func SearchReading(q interface{}, limit int) (searchResults []Reading,

How to represent an array with mixed types

你。 提交于 2019-12-07 01:26:00
问题 I am constructing an aggregation pipeline query with the $substr command from MongoDB but I don't know how to represent the array it requires in Go with the mgo driver because it contains different types of values (string, int). Here is the query in javascript: [ {$group: {"_id": {"dt": {"$substr": ["$dt",0,6]}}}} ] What this is trying to do is get the substring of dt (from the previous stage of aggregation) with starting index 0 and ending index 6. In Go i got: []bson.M{"$group": bson.M{"_id

Case insensitive MongoDB query from Go

孤街醉人 提交于 2019-12-06 15:32:25
I have this json file: [{ "name": "chetan", "age": 23, "hobby": ["cricket", "football"] }, { "name": "raj", "age": 24, "hobby": ["cricket", "golf"] }] I use this Go code to search the data: id := "ket" regex := bson.M{"$regex": bson.RegEx{Pattern: id}} err = c.Find(bson.M{"hobby": regex}).All(&result) It finds if searched by the same string like "cricket" but if I search string like this "Cricket", it does not find it. Add Options: "i" to your RegEx. bson.M{"$regex": bson.RegEx{Pattern: id, Options: "i"}} 来源: https://stackoverflow.com/questions/37340539/case-insensitive-mongodb-query-from-go

$literal usage in Golang-mgo

一笑奈何 提交于 2019-12-06 14:38:33
I can't understand how to properly use $literal. I am using mgo.v2 and mgo.v2/bson package. db.store.aggregate([ {"$project":{ "location":{ "type":{"$literal":"Point"}, "coordinates":["$longitude","$latitude"] }} },]) I used the above code to fetch data in mongodb and working fine.It gives me the result { "location":{ "type":"Point", "coordinates":[77.587073,12.958794] }} I tried to use the same in golang and it is shown below pipe :=DB.C("store").Pipe([]bson.M{ {"$project":bson.M{"location": bson.M{"type": bson.M{"$literal":"Point"},"coordinates":[]interface{}{"$longitude","$latitude"}}}}}