Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mongodb管道查询$project中的错误应该是什么?_Mongodb_Go - Fatal编程技术网

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

mongodb管道查询$project中的错误应该是什么?,mongodb,go,Mongodb,Go,我正在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.LogColle

我正在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)
}
在值中会出现
字符串
为什么这不会给我任何结果。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"
 }
}
结构如下所示:

 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

我不懂这种语言,但是:

  • 如果您传递bson.M{“$unwind”:“$booking\u details”},生成的文档将有“booking\u details”字段

  • 所以你必须改变

     "booking":1,
     "booking_values":bson.M{"$objectToArray":"$booking"},
    

     "booking_details":1,
     "booking_values":bson.M{"$objectToArray":"$booking_details"},

将管道的
代码更改为以下代码:-

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\u details
字段,因此您必须删除它,然后运行您的go代码。

没有
booking\u details
字段,您尝试
$unwind
它。。。因此,这将过滤掉所有结果。@icza您所说的
unwind
Booking\u值
给出的文档样本是来自unwind阶段的结果,还是您的基本文档?