我正在管道中的数据在mongodb集合中,但它不会向我返回任何数据,代码如下:-
func GetLog(c *gin.Context) {
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{"$unwind": "$booking_details"},
bson.M{"$project": bson.M{
"role":1,
"date":1,
"idadress":1,
"booking":1,
"booking_values":bson.M{"$objectToArray":"$booking"},
} },
bson.M{"$match": bson.M{
"booking_values.v" : bson.RegEx{"(?i).*"+values+".*", "i"},
}}, } )
fmt.Println(pipe)
err := pipe.All(&result)
fmt.Println(result)
}
在价值观中会有string
为什么这不会给我任何结果的原因。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": "[email protected]",
"provider": "provider1",
"address": "brazil",
"appt": "123456",
"phone": "987654321"
}
}
结构如下所示:
type Log struct {
Id bson.ObjectId `json:"_id" bson:"_id,omitempty"`
Role string `json:"role" bson:"role"`
Date string `json:"date" bson:"date"`
IpAddress string `json:"idaddress" bson:"idaddress"`
Booking interface{} `json:"booking" bson:"booking"`
}
type Logs []Log
将您的pipe
代码更改为以下代码:-
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{
"booking_values.v" : bson.RegEx{"(?i).*"+values+".*", "i"},
}}, } )
到您没有icza所说的任何booking_details
字段,因此您必须将其删除,然后运行go代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句