-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathq.go
85 lines (67 loc) · 2.26 KB
/
q.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"os"
"github.com/efenstakes/walmart-api-g/products"
"github.com/joho/godotenv"
"github.com/kamva/mgm/v3"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
// this is always called before main making it a great place to initialize
func inite() {
if err := godotenv.Load(); err != nil {
panic("Couldn't load variables from environment")
}
err := mgm.SetDefaultConfig(
nil, "walmart", options.Client().ApplyURI(os.Getenv("DB_URI")),
)
if err != nil {
panic("Could not connect to MongoDB")
} else {
fmt.Println("Connected to db")
}
}
func maine() {
product := products.Product{}
err := mgm.Coll(&products.Product{}).FindByID("64b6f2999debdaa3cd5ef947", &product)
if err != nil {
fmt.Println("Product Not Found")
}
fmt.Println("product id ", product.ID.Hex())
fmt.Println("product name ", product.Name)
if product.Name == "" {
fmt.Println("Product Not Found")
}
return
productIds := []string{}
productIds = append(productIds, "64b6f2999debdaa3cd5ef647")
productIds = append(productIds, "64b6e57808b7e2e7ed102442")
// filters := bson.D{{"id", bson.D{{"$in", bson.A{"64b6f2999debdaa3cd5ef647"}}}}}
// cursor, err := mgm.Coll(&products.Product{}).Find(mgm.Ctx(), filters)
// cursor, err := mgm.Coll(&products.Product{}).Find(mgm.Ctx(), bson.M{"id": bson.M{"$in": productIds}})
filters := map[string]primitive.ObjectID{}
productObjectIds := []primitive.ObjectID{}
for _, productID := range productIds {
objID, _ := primitive.ObjectIDFromHex(productID)
filters["_id"] = objID
productObjectIds = append(productObjectIds, objID)
}
cursor, err := mgm.Coll(&products.Product{}).Find(mgm.Ctx(), bson.M{"_id": bson.M{"$in": productObjectIds}})
// filters["_id"] = objID // "64b6f2999debdaa3cd5ef647"
// cursor, err := mgm.Coll(&products.Product{}).Find(mgm.Ctx(), filters)
if err != nil {
fmt.Println("error ", err.Error())
}
orderProducts := []products.Product{}
if err := cursor.All(mgm.Ctx(), &orderProducts); err != nil {
fmt.Println("An Error in Fetch Cursor")
fmt.Println("error ", err.Error())
}
fmt.Println(orderProducts)
fmt.Println("Your order has these products")
for _, v := range orderProducts {
fmt.Println(v.Name)
}
}