mongo-go

MongoDB list databases with given prefix in Go

时间秒杀一切 提交于 2020-06-27 18:47:11
问题 Question How can I list databases only with the given prefix ( prefix_ )? Example: package main import ( "context" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "log" ) type foo struct { Value string } func main() { clientOptions := options.Client().ApplyURI("mongodb://10.0.12.76:27018") client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatal(err) } db := [3]string{"prefix_foo", "prefix

Go converting ptypes/struct Value to BSON

跟風遠走 提交于 2020-03-23 09:50:08
问题 Requirements Two services: Server - for writing blog posts to MongoDB Client - sends request to the first service The blog post has title of type string , and content which is a dynamic type - can be any JSON value. Protobuf syntax = "proto3"; package blog; option go_package = "blogpb"; import "google/protobuf/struct.proto"; message Blog { string id = 1; string title = 2; google.protobuf.Value content = 3; } message CreateBlogRequest { Blog blog = 1; } message CreateBlogResponse { Blog blog =

How to filter fields from a mongo document with the official mongo-go-driver

耗尽温柔 提交于 2020-02-15 08:36:08
问题 How can I filter fields with the mongo-go-driver. Tried it with findopt.Projection but no success. type fields struct { _id int16 } s := bson.NewDocument() filter := bson.NewDocument(bson.EC.ObjectID("_id", starterId)) var opts []findopt.One opts = append(opts, findopt.Projection(fields{ _id: 0, })) staCon.collection.FindOne(nil, filter, opts...).Decode(s) In the end, I want to suppress the field "_id". But the documents didn't change. 回答1: The reason why it doesn't work for you is because

Why am I getting all zero value for certain field in my json from mongodb?

你离开我真会死。 提交于 2020-01-30 13:13:33
问题 I'm fetching my data from MongoDB atlas in a Go web Server using the official mongodb-go-driver. I'm using json.Marshal to convert to json. but all values of certain fields becomes Zero. package main import ( "context" "fmt" "log" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" ) var c = GetClient() type PlantData struct { Minute int `json:"minute"` Date

Why am I getting all zero value for certain field in my json from mongodb?

≯℡__Kan透↙ 提交于 2020-01-30 13:13:28
问题 I'm fetching my data from MongoDB atlas in a Go web Server using the official mongodb-go-driver. I'm using json.Marshal to convert to json. but all values of certain fields becomes Zero. package main import ( "context" "fmt" "log" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" ) var c = GetClient() type PlantData struct { Minute int `json:"minute"` Date

mongo-go-driver fails with Server Selection Timeout when using MongoDB Atlas

与世无争的帅哥 提交于 2020-01-15 03:18:10
问题 Go Version: 1.12.5 I have this code which uses the node.js mongo driver const MongoClient = require('mongodb').MongoClient; const uri = process.env.MONGO_HOST + "dbname?retryWrites=true"; const client = new MongoClient(uri, { useNewUrlParser: true }); client.connect(async (err) => { if (err) { throw err } const collection = client.db("dbname").collection("collectionName"); const cursor = collection.find() await cursor.forEach(console.log) // perform actions on the collection object client

How to parse extended JSON Date in aggregation pipeline using ParseExtJSONArray() in mongo-go-driver

两盒软妹~` 提交于 2020-01-06 02:46:14
问题 I've got a collection with a Date field: { "_id" : ObjectId("5b92b359ddceef5b24502834"), "dateTimeGMT" : ISODate("2018-08-22T09:29:25.000Z"), yada, yada, yada } I'm trying to find by date in a $match aggregation stage with the ParseExtJSONArray function of mongo-go-driver. (I am aware of how to do this with *bson.Array directly. I'm asking so I know the right way to do it with ParserExtJSONArray or if I've run up against a limitation.) I've simplified to this example and confirmed it is

mongo-go-driver aggregate query always return “Current”: null

[亡魂溺海] 提交于 2020-01-03 04:01:13
问题 I want to sum a field by using pipeline := []bson.M{ bson.M{"$group": bson.M{ "_id": "", "count": bson.M{ "$sum": 1}}} } ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) result, err := collection.Aggregate(ctx, pipeline) but it always return "Current": null Any solution? 回答1: First, Collection.Aggregate() returns a mongo.Cursor, not the "direct" result. You have to iterate over the cursor to get the result documents (e.g. with Cursor.Next() and Cursor.Decode()), or use