I'm using golang and its official mongo db driver, and I want to save documents is in the following structure:
type BlacklistRecord struct {
ID string `bson:"_id" json:"id"`
Type string `bson:"type" json:"type"`
Value string `bson:"value" json:"value"`
Source map[string]int `bson:"source" json:"source"`
LastUpdate string `bson:"lastUpdate" json:"lastUpdate"`
}
this is what is saved into database as a sample:
{
_id: '1b836f704c884d28',
type: 'url',
value: 'smtp.clarinda.bluehornet.com',
source: {
'https://hostfiles.frogeye.fr/firstparty-trackers-hosts.txt': 1
},
lastUpdate: '2022-05-18 13:30:44.425104695 +0000 UTC m=+624.684836025'
}
What I want to do is searching for documents which at least one of their sources contains a sub string (case insensitive). The source value is a map itself, which its key is the source URL and the value is the number of repeats in that source url. I have tried a lot but I couldn't do much. I know I can use:
key := bson.M{
"$regex": primitive.Regex{
Pattern: ".*" + value + ".*", Options: "i",
}
this only works for value of key. what about search for the key itself? for example if someone give me "hosTfiLes" I should return the records which inside the source field of them, a key with this expression (case insensitive) exists. Thank you for your helps.