Sequential queries with golang & mongodb - mongodb

Wondering what is best way to make sequential queries from Golang for a mongodb.
Example lets say you have :
result *bson.M
ids:=["543d171c5b2c12420dd016","543d171c5b2dd016"]
oids := make([]bson.ObjectId, len(ids))
for i := range ids {
oids[i] = bson.ObjectIdHex(ids[i])
}
query := bson.M{"_id": bson.M{"$in": oids}}
error:= c.Find(query).All(&result)
And you want to take the output of the _ids and use it as a query for another table.
So is this correct?
query = bson.M{"_id": bson.M{"$in": result}}

Here's how to construct a query using the ids of documents returned from some other query.
var docs []bson.M
if err := c.Find(query).All(&docs); err != nil {
// handle error
}
docIDs := make([]interface{}, len(docs))
for i := range docs {
docIds[i] = docs[i]["_id"]
}
query = bson.M{"_id": bson.M{"$in": docIDs}}

Related

How to update many by objectid in mongodb using go driver

I want to bulk update collection by slice of objectid
Here is the function for updatemany
func BulkDeleteEmailContentFieldByIds(ids []primitive.ObjectID) error {
filter := bson.M{"_id": bson.M{"$in": ids}}
update := bson.D{{"$unset", bson.D{{"email_content", 1}}}}
result, err := collection.UpdateMany(ctx, filter, update)
if err != nil {
return err
}
pp.Printf("Bulk delete removed %v document(s)\n", result.ModifiedCount)
return nil
}
var trackingLogIds []primitive.ObjectID
for _, v := range userDocs {
wg.Add(1)
go func(v bson.M) {
defer wg.Done()
// do something to return objectID
trackingLogIds = append(trackingLogIds, emaDoc["_id"].(primitive.ObjectID))
}(v)
}
emadb.BulkDeleteEmailContentFieldByIds(trackingLogIds)
Is the slice trackingLogIds is the correct way to search for documents with objectId
And is the query for updatemany i doing is right?
If not then how can i improve it.
Thanks
I had test the function i provided and its working as expected.
Find all document in slice of objectid and unset the field email_content

filter query mongoldb Golang

I am trying to get a list of data that match specific queries but I am getting this error
"(AtlasError) merchant is not allowed or the syntax is incorrect, see
the Atlas documentation for more information"
func ...
var result []*model.Package
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
tokenData, err := middleware.CtxValue(ctx)
if err != nil {
return nil, err
}
orQuery := []bson.M{}
merchant := "abc"
completedQuery := bson.M{"status": "completed"}
cancelledQuery := bson.M{"status": "cancelled"}
orQuery = append(
orQuery,
cancelledQuery,
completedQuery)
limit64 := int64(limit)
page64 := int64(page)
match := bson.M{"$match": bson.M{"$nor": orQuery}}
var filterQuery primitive.M
if tokenData.Role == "admin" && merchant != nil {
filterQuery = bson.M{"merchant": bson.M{"id": merchant}}
} else {
filterQuery = bson.M{"user": bson.M{"id": tokenData.Id}}
}
paginatedData, err1 := paginate.New(r.Collection).Context(ctx).Limit(limit64).Page(page64).Aggregate(match, filterQuery)
if err1 != nil {
return nil, err1
}
...
filterQuery, which seems to contain { "merchant" : { "id" : "abc" } }, is being passed sepearately to .Aggregate(). But the aggregation framework is expecting to receive something that represents a sequence of pipeline stages. Each of these stages, outlined here in the documentation, are expected to begin with a $ character such as the $match stage.
Currently the database is attempting to process merchant as an options for the pipeline (see here and here). But such an option doesn't exist, hence the error message.
To resolve this, you should incorporate the filterQuery logic into the existing match variable/stage that you are building and passing. Alternatively you can wrap filterQuery in a different $match and then pass both of them (as a single argument) to .Aggregate().
This example in the documentation shows they build multiple stages and then submit them together to .Aggregate() via mongo.Pipeline{...}:
// create the stages
matchStage := bson.D{{"$match", bson.D{{"toppings", "milk foam"}}}}
unsetStage := bson.D{{"$unset", bson.A{"_id", "category"}}}
sortStage := bson.D{{"$sort", bson.D{
{"price", 1},
{"toppings", 1}},
}}
limitStage := bson.D{{"$limit", 2}}
// pass the stage into a pipeline
// pass the pipeline as the second paramter in the Aggregate() method
cursor, err := coll.Aggregate(context.TODO(), mongo.Pipeline{matchStage, unsetStage, sortStage, limitStage})

How to find result with mongo-driver by objectid field

I have 2 structs:
type Customer struct {
MyID int `bson:"myID"`
}
type Bag struct {
Customer primitive.ObjectID `bson:"customer"`
}
But I dunno how to make filter to find document in Bag collection by Customer.MyID
I tried something like this:
filter := bson.D{{"customer", bson.D{{"myID", 123456789}}}}
cur, err := collection.Find(context.TODO(), filter)
Also tried this:
filter := bson.D{{"customer.myID", bson.D{{"$eq", 123456789}}}}
cur, err := collection.Find(context.TODO(), filter)
And I always get nothing in my cur variable. Please help me how to use it right.
The collection name must not appear in the filter, the collection on which you call Find() already represents (or should) the customer collection, so just use:
collection := client.Database("yourdatabase").Collection("customer")
filter := bson.M{
"myID": 123456789,
}
cur, err := collection.Find(context.TODO(), filter)
Or with bson.D:
filter := bson.D{{"myID", 123456789}}

Checking if data exists in a mongodb collection in goLang?

If i want to check if there currently exists at least one document in a collection, how would I go about doing this in GoLang?
The most performant way to check if documents exist in a collection is to use the EstimatedDocumentCount function on a collection because it gets an estimate from the collection's metadata.
You can do something like this:
count, err := collection.EstimatedDocumentCount(context.Background())
If the actual count of documents in the collection is important and you need more than just an estimate, it makes sense to look into the MongoDB aggregation framework.
You can do something like this which wraps the aggregation framework:
count, err := collection.CountDocuments(ctx, bson.M{})
if err != nil {
panic(err)
}
if count >= 1 {
fmt.Println("Documents exist in this collection!")
}
You could also try something like the following if you want to use the aggregation framework directly:
cursor, err := episodesCollection.Aggregate(ctx, []bson.D{
bson.D{{"$count", "mycount"}},
})
if err != nil {
panic(err)
}
var counts []bson.M
cursor.All(ctx, &counts)
fmt.Println(counts[0]["mycount"])

Why does my mongodb query return 0 results?

This is my database collection:
With this go code, I try to get all the users who are either involved in the story or created the story with the given id.
func main() {
for stf.DB == nil {
}
collection := stf.DB.Collection("user")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
fter := bson.M{"_id": id}
involvedFilter := bson.M{"stories_involved": fter}
createdFilter := bson.M{"stories_created": fter}
filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}
cur, err := collection.Find(ctx, filter)
if err != nil {
log.Fatal(err.Error())
}
defer cur.Close(ctx)
for cur.Next(ctx) {
var result bson.M
err := cur.Decode(&result)
if err != nil {
log.Fatal(err.Error())
}
fmt.Println(result)
}
if err := cur.Err(); err != nil {
log.Fatal(err.Error())
}
}
The code doesn't output any errors, but it also doesn't output any objects...
Thanks for your help in advance!
Your query translates into:
{"$or":[
{"stories_involved":{
"_id": ObjectId("5cb4dd7e29d3dca573a73d4c")}},
{"stories_created":{
"_id":ObjectId("5cb4dd7e29d3dca573a73d4c")}}
]}
Which means that it's searching for either a document with a nested document
i.e:
{stories_involved: {_id: <value>}} OR {stories_created: {_id: <value>}}.
However, the documents in the collection contains nested document array i.e:
{stories_involved: [{_id:<value>}]} OR {stories_created: [{_id:<value>}]}
This is the reason your query is not returning any value (and no error because the query syntax is correct).
There are two ways of Querying a document nested in an array using dot notation. If you know the index of the array for the document, you can just specify the position:
id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
involvedFilter := bson.M{"stories_involved.0._id": id}
createdFilter := bson.M{"stories_created.0._id": id}
filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}
cur, err := collection.Find(ctx, filter)
If you do not know the index position of the document nested in the array, concatenate the name of the array field, with a dot (.) and the name of the field in the nested document:
id, _ := primitive.ObjectIDFromHex("5cb4dd7e29d3dca573a73d4c")
involvedFilter := bson.M{"stories_involved._id": id}
createdFilter := bson.M{"stories_created._id": id}
filter := bson.M{"$or": []bson.M{involvedFilter, createdFilter}}
cur, err := collection.Find(ctx, filter)
See also MongoDB: Query Documents