mgo

Mongodb - aggregation $push if conditional

天涯浪子 提交于 2020-01-10 23:45:24
问题 I am trying to aggregate a batch of documents. There are two fields in the documents I would like to $push. However, lets say they are "_id" and "A" fields, I only want $push "_id" and "A" if "A" is $gt 0. I tried two approaches. First one. db.collection.aggregate([{ "$group":{ "field": { "$push": { "$cond":[ {"$gt":["$A", 0]}, {"id": "$_id", "A":"$A"}, null ] } }, "secondField":{"$push":"$B"} }]) But this will push a null value to "field" and I don't want it. Second one. db.collection

Using mgo with context

*爱你&永不变心* 提交于 2020-01-07 04:40:21
问题 I have been using mgo for my API but I'm seeing many current connections in my MongoDB (while using less than 5 devices for testing). By executing db.serverStatus().connections in my Mongo server I get: { "current" : 641, "available" : 838219, "totalCreated" : 1136 } . Below I transcript my issue in mgo's Github (Issue #429): Is my way of using mgo in a web server the correct way? If not, can you give me a full example? This code is not functional, take it as almost pseudo code (because of

Go and MongoDB connection won't work with panic log “no reachable server”

雨燕双飞 提交于 2020-01-05 05:10:33
问题 I am using mGo as a driver for my Go Web App to another MongoDB system. So I am not running Mongo on the same system. (URL is not localhost). However, I get "panic: no reachable servers" error. Here is the test function that runs right when the Go server starts: dialInfo, err0 := mgo.ParseURL("mongodb://1234MY456IP:27017,27018") if err0 != nil { panic(err0) } dialInfo.Direct = true dialInfo.FailFast = true session, err := mgo.DialWithInfo(dialInfo) if err != nil { panic(err) } defer session

using := gives unused error but using = don't in Go

一世执手 提交于 2020-01-03 06:01:06
问题 I have piece of code in which I get error when I use := but when I use = it compiles properly. What I learned is that := only requires only atleast one variable to be defined, others need not be defined, but considering this code is it a bug in Go? Uncompilable code: Error: services/db_service.go:16: Session declared and not used package services import ( "gopkg.in/mgo.v2" "log" ) const DB = "mmdb_dev" var Session *mgo.Session func InitMongo() bool { url := "mongodb://localhost" log.Println(

How do I deal with an arbitrary hash returned from mongo in go (using mgo)?

左心房为你撑大大i 提交于 2020-01-03 05:22:28
问题 All of the references I can find construct a struct to hold the return values, assuming that each returned record has the same schema. If they're really documents and don't have a consistent schema other than maybe a few queryable consistent metadata attributes, how can I handle that return value? e.g.: this https://groups.google.com/forum/#!msg/mgo-users/KirqfCSlKFc/t2l3l4yxFRwJ assumes that you have an array of timestamps. What if it's an array where some of the values are timestamps and

Go语言mgo

牧云@^-^@ 提交于 2019-12-26 07:10:06
本文重点介绍mgo使用,仅简单介绍mongodb。 mongodb特性 mongdb简单介绍 注意: 上图已经告知我们mongo不支持事务,在开发项目应用时,想要保证数据的完整性请考虑关系型数据库(经典例子银行转账)。 mongo提供了许多原子操作,比如文档的保存,修改,删除等,都是原子操作。所谓原子操作就是要么这个文档保存到mongodb,要么没有保存到mongodb,不会出现查询到的文档不完整的情况。 mgo简介 mgo 是 mongodb 的 GO 语言驱动包。 mgo官网: http://labix.org/mgo mgo使用 mgo方案一 package mgo import ( "flag" "gopkg.in/mgo.v2" "log" "study/conf" ) var session *mgo.Session var database *mgo.Database func init() { /*配置mongodb的josn文件,配置内容如下: { "hosts": "localhost", "database": "user" }*/ filename := flag.String("config", "./conf/config.json", "Path to configuration file") flag.Parse() config := &conf

Sending chunked files to save in mongodb

南笙酒味 提交于 2019-12-25 17:54:13
问题 I have 2 different servers for example (Server 1 , Server 2), In the first server I have golang app which splits files and sending to second server which should save in mongodb via mgo.v2 Server 1: func mainHandle(rw http.ResponseWriter, rq *http.Request) { fileToBeChunked := "/Users/IT/Desktop/4k.jpg" file, err := os.Open(fileToBeChunked) if err != nil { fmt.Println(err) os.Exit(1) } defer file.Close() fileInfo, _ := file.Stat() var fileSize int64 = fileInfo.Size() const fileChunk = 0.25 *

Sending chunked files to save in mongodb

醉酒当歌 提交于 2019-12-25 17:53:08
问题 I have 2 different servers for example (Server 1 , Server 2), In the first server I have golang app which splits files and sending to second server which should save in mongodb via mgo.v2 Server 1: func mainHandle(rw http.ResponseWriter, rq *http.Request) { fileToBeChunked := "/Users/IT/Desktop/4k.jpg" file, err := os.Open(fileToBeChunked) if err != nil { fmt.Println(err) os.Exit(1) } defer file.Close() fileInfo, _ := file.Stat() var fileSize int64 = fileInfo.Size() const fileChunk = 0.25 *

Is there a way to get slice as result of Find()?

你离开我真会死。 提交于 2019-12-25 17:37:45
问题 Now I'm doing: sess := mongodb.DB("mybase").C("mycollection") var users []struct { Username string `bson:"username"` } err = sess.Find(nil).Select(bson.M{"username": 1, "_id": 0}).All(&users) if err != nil { fmt.Println(err) } var myUsers []string for _, user := range users{ myUsers = append(myUsers, user.Username) } Is there a more effective way to get slice with usernames from Find (or another search function) directly, without struct and range loop? 回答1: The result of a MongoDB find() is

如何用“ like”查询MongoDB?

混江龙づ霸主 提交于 2019-12-25 09:42:10
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我想查询一些与SQL的 like 查询: SELECT * FROM users WHERE name LIKE '%m%' 我如何在MongoDB中实现相同目标? 我在 文档中 找不到 like 的运算符。 #1楼 在 使用 Python的 PyMongo 猫鼬 使用 Node.js Jongo ,使用 Java MGO, 用 围棋 你可以做: db.users.find({'name': {'$regex': 'sometext'}}) #2楼 您可以使用where语句来构建任何JS脚本: db.myCollection.find( { $where: "this.name.toLowerCase().indexOf('m') >= 0" } ); 参考: http : //docs.mongodb.org/manual/reference/operator/where/ #3楼 如果使用 node.js , 则 表示您可以编写以下代码: db.collection.find( { field: /acme.*corp/i } ); //or db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } ); 另外