mongo-go

bson.D vs bson.M for find queries

孤街醉人 提交于 2021-02-19 04:20:05
问题 This specifc question is in relation to using mongodb with the golang package mongo-driver, but I would assume this applies across most interfaces with mongodb. When using Find to query some data from a collection, we can use both the bson.M- and bson.D-type to specify the filter for this find. As per the documentation bson.D should be used if the order of elements matters and bson.M should be used otherwise. D is an ordered representation of a BSON document. This type should be used when the

How to perform addToSet using Go official driver?

别来无恙 提交于 2021-02-15 07:09:15
问题 I need to do addToSet operation using official Go MongoDB driver. In MongoDB we have some docs: { _id: 2, item: "cable", tags: [ "electronics", "supplies" ] } Then to perform addToSet : db.inventory.update( { _id: 2 }, { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } } ) Result: { _id: 2, item: "cable", tags: [ "electronics", "supplies", "camera", "accessories" ] } 回答1: $addToSet is an update operation, if you want to update a single document, you may use the

How to perform addToSet using Go official driver?

守給你的承諾、 提交于 2021-02-15 07:08:34
问题 I need to do addToSet operation using official Go MongoDB driver. In MongoDB we have some docs: { _id: 2, item: "cable", tags: [ "electronics", "supplies" ] } Then to perform addToSet : db.inventory.update( { _id: 2 }, { $addToSet: { tags: { $each: [ "camera", "electronics", "accessories" ] } } } ) Result: { _id: 2, item: "cable", tags: [ "electronics", "supplies", "camera", "accessories" ] } 回答1: $addToSet is an update operation, if you want to update a single document, you may use the

Golang GraphQL MongoDB Struggling to get date and id out of the Database

喜欢而已 提交于 2021-02-11 15:40:51
问题 I am new to Golang and Graphql so I probably messed a lot of the configuration up but I am struggling to get values returned from my database using the GraphQL API I created. Whenever I query my database using the GraphQL API I created in Golang It throws the error cannot decode UTC datetime into a string type and struggles to get the id out. Here is my GrapqhQL schema: type User { _id: ID! username: String! passwordHash: String! email: String! userInfo: userStats profileStats: profileInfo }

Necessity of bson struct tag when using with MongoDB go client

本小妞迷上赌 提交于 2021-02-11 15:31:40
问题 I am watching a tutorial on how to create Go restful APIs that use MongoDB for persistence (this one to be more precise). The instructor is using in his model (struct) both json and bson tags, something like type NoteUpdate struct { ID string `json:"id,omitempty" bson:"_id,omitempty"` Title string `json:"title" bson:"title,omitempty"` Content string `json:"content" bson:"content,omitempty"` ChangedAt int64 `json:"changed_at" bson:"changed_at"` } However the official go driver example does not

Necessity of bson struct tag when using with MongoDB go client

别来无恙 提交于 2021-02-11 15:30:22
问题 I am watching a tutorial on how to create Go restful APIs that use MongoDB for persistence (this one to be more precise). The instructor is using in his model (struct) both json and bson tags, something like type NoteUpdate struct { ID string `json:"id,omitempty" bson:"_id,omitempty"` Title string `json:"title" bson:"title,omitempty"` Content string `json:"content" bson:"content,omitempty"` ChangedAt int64 `json:"changed_at" bson:"changed_at"` } However the official go driver example does not

With mongo-go-driver, how do I efficiently retrieve duplicated field name from WriteError?

和自甴很熟 提交于 2021-02-09 09:25:59
问题 I have three unique indexes in my collection. When user accidentally insert a data having a duplicate in the field B , how do I know that the duplication comes from field B ? On unique index constraint violation, mongo-go-driver behavior is returning err WriteException , which basically consist of an array of WriteError and some other object. The WriteError itself (from mongo-go-driver) : // WriteError is an error that occurred during the execution of a write operation. This error type is

mongo-go-driver: nested OR/AND query filter

╄→гoц情女王★ 提交于 2021-02-05 08:15:26
问题 I try to create a MongoDB query filter with the nested operators (OR/AND/...). But lib requires to create a bson.D and pass bson.E elements into it. If I need to have OR/AND inside AND/OR - I need to put bson.M + bson.D inside bson.D like this: filter := bson.M{"$and": bson.D{{"p", 10}, bson.M{"$or": bson.D{{"s", 30}, {"a", 1}}}}} .. and of course it doesn't work: cannot use primitive.M literal (type primitive.M) as type primitive.E in slice literal . Probably the same problem will happen if

missing type in composite literal go AND missing key in map literal go

橙三吉。 提交于 2021-01-29 05:13:44
问题 Im trying to do Pagination with MongoDB I write this code: findOptions := options.Find() findOptions.SetLimit(20) findOptions.SetSort(bson.M{{"_id", 1}}) cursor, err34 := collection.Find(context.Background(), bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions) Now It keeps complaining: missing type in composite literal go AND missing key in map literal go It complains for this part: findOptions.SetSort(bson.M{{"_id", 1}}) and bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions) I'm

How to run mongo command with mongo-go-driver?

不打扰是莪最后的温柔 提交于 2021-01-27 19:11:04
问题 Hi there :) I'm working on a golang app linked to mongo DB (I use the official driver: mongo-go ) and here's my problem,I want to execute this function db.rmTickets.find().forEach(function(doc) { doc.created=new Date(doc.created) doc.updated=new Date(doc.updated) doc.deadline=new Date(doc.deadline) doc.dateEstimationDelivery=new Date(doc.dateEstimationDelivery) doc.dateTransmitDemand=new Date(doc.dateTransmitDemand) doc.dateTransmitQuotation=new Date(doc.dateTransmitQuotation) doc