如何在golang中绑定mongodb的数据?

时间:2018-07-09 04:19:41

标签: mongodb go

我正在从mongodb的日志记录中检索数据。以下是我正在检索的数据之类的数据示例:

{
 "_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"
 }
}

我想将全部数据绑定到一个名为search_values的字段名称中。当我想从中搜索任何数据时,它将搜索所有字段,同时也看到bookingadding对象。以下是我用于搜索的代码:

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)

请帮助我解决此问题。谢谢您对我的帮助。

0 个答案:

没有答案