How to use golang mongodb select

my code here

var c1 Category
	var setElements bson.D
	if len(c.CategoryLargeType) > 0 {
		setElements = append(setElements, bson.E{Key: "category_large_type", Value: c.CategoryLargeType})
	}
	if len(c.CategoryMiddleType) > 0 {
		setElements = append(setElements, bson.E{Key: "category_middle_type", Value: c.CategoryMiddleType})
	}
	if len(c.CategorySmallType) > 0 {
		setElements = append(setElements, bson.E{Key: "category_small_type", Value: c.CategorySmallType})
	}
	setMap := bson.D{
		{Key: "$in", Value: setElements},
	}
	fmt.Println(setMap)
	err := categoryCollection.FindOne(ctx, setMap).Decode(&c1)
	fmt.Println(c1)
	fmt.Println(err)
	if err != nil {
		return http.StatusInternalServerError, Response{Status: http.StatusInternalServerError, Mssage: "데이터 변환에 실패하였습니다.", Result: ""}
	}
	return http.StatusOK, Response{Status: http.StatusInternalServerError, Mssage: "이미 저장된 데이터가 존재합니다.", Result: c1}

example) select * from TEST where id ="" AND title="" AND desc =""

how to use example for golang mongodb Find

my error code here
unknown top level operator: $in. If you have a field name that starts with a ‘$’ symbol, consider using $getField or $setField

Hi @kindcode and welcome in the MongoDB Community :muscle: !

You have a code sample in the doc here:

Cheers,
Maxime.

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.