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

时间:2018-07-06 10:34:25

标签: 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.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": "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

2 个答案:

答案 0 :(得分:1)

将您的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代码。

答案 1 :(得分:0)

不懂这种语言,但是:

  • 如果您通过bson.M {“ $ unwind”:“ $ booking_details”},则生成的文档将具有“ booking_details”字段,

  • 所以您必须更改

    df['First_day'] = df['Date'] - df['Date'].dt.weekday * np.timedelta64(1, 'D')
    
    print(df)
    
      Item       Date  First_day
    0    A 2018-08-03 2018-07-30
    1    B 2018-08-20 2018-08-20
    

通过

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