首页 > 解决方案 > golang如何绑定mongodb的数据?

问题描述

我正在从日志的 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. 当我想从中搜索任何数据时,它会搜索所有字段,也可以看到bookingandadding对象。下面是我用于搜索的代码:

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)

请帮我解决这个问题。谢谢你帮助我进阶。

标签: mongodbgo

解决方案


推荐阅读