drbz99867 2018-07-06 10:34
浏览 62
已采纳

mongodb管道查询$ project中的错误应该是什么?

I'm pipeline my data in the mongodb collection but it will not returning any data to me the code is following:-

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)
}

In the values there will be string why this will not giving me any result. the data in mongodb is:

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

The struct is like given below:

 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
  • 写回答

2条回答 默认 最新

  • doucheng9634 2018-07-06 11:03
    关注

    Change your pipe code to the below code:-

    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"},
        }}, } )
    

    to you don't have any booking_details field as the icza said so you have to remove it and then run your go code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么