I'm retrieving the data from mongodb record of the logs. Below is the example of the data like data I'm retrieving:
{
"_id": ObjectId("5b3d970398e9d099427896c3"),
"role": "New Booking is there by abc",
"date": "07/04/2018",
"idaddress": "213.123.123.213",
"booking": {
"bedroom": 4,
"bathroom": 6,
"customer": "abc",
"email": "abc@gmail.com",
"provider": "provider1",
"address": "brazil",
"appt": "123456",
"phone": "987654321"
},
"adding":{
"user": "abcUser",
"sell": "Seller"
}
}
I want to bind this whole data into a one field name called search_values
. And when I want to search any data from it then it will search all fields also see the booking
and adding
object too. Below is my code that I'using for searching:
values := c.Query("value")
fmt.Println("value", values)
result := []bson.M{}
mongoSession := config.ConnectDb()
getCollection := mongoSession.DB(config.Database).C(config.LogCollection)
pipe := getCollection.Pipe([]bson.M{
bson.M{"$project": bson.M{
"role":1,
"date":1,
"idaddress":1,
"booking":1,
"booking_values":bson.M{"$objectToArray":"$booking"},
} },
bson.M{"$match": bson.M{"$or": []bson.M{
bson.M{"booking_values.v": bson.RegEx{"(?i).*" + values + ".*", "i"}},
bson.M{"role": bson.RegEx{"(?i).*" + values + ".*", "i"}},
bson.M{"date": bson.RegEx{"(?i).*" + values + ".*", "i"}},
bson.M{"idaddress": bson.RegEx{"(?i).*" + values + ".*", "i"}},
}}})
fmt.Println(pipe)
err := pipe.All(&result)
fmt.Println(result)
Please help me to solve this problem. Thank you for helping me in advanced.