mongodb - 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
. 当我想从中搜索任何数据时,它会搜索所有字段,也可以看到booking
andadding
对象。下面是我用于搜索的代码:
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)
请帮我解决这个问题。谢谢你帮助我进阶。
解决方案
推荐阅读
- reactjs - 尽管 setState,Semantic React Dropdown 不显示值
- java - 从方法返回的同步代码中的对象
- ios - Swift:消息过滤器扩展不起作用
- c - 要构建的正确返回类型是什么?
- python - pytorch 使用标签进行自定义梯度更新
- javascript - 按下主 GUI 上的按钮时,即使步骤正确执行,编辑 GUI JDialog 也不会打开
- python - 如何在python中为冒泡排序算法添加反向功能
- android - 如何从房间数据库中获取与 recyclerView 中单击项目相关的数据到新活动?
- python - 如何在熊猫滚动对象上应用分配?
- java - 将照片保存到图库时出错 - Android Studio (java)