如何使用查询结构化 bson 查询,在何处使用 go

How to use structure a bson query with query,where and in using go

我想根据时间范围和 $in 中使用的字段获取文档条目。我可以使用 mongoose 和 nodejs 很容易地做到这一点,就像这样,其中 param 是键,params 是一个数组

Data.find( 
         {
          timestamp:
                 {
          $gte: new Date(query.startDate),
          $lt: new Date(query.endDate)
               }
         }
       ).where(`${param}`).in(params);

下面是我使用 go 和 bson 对 bson 的尝试。我发现很难构建查询

aliases := []string {"000","111"}
query := bson.M{
        "datetime": bson.M{
            "$gt": startDate,
            "$lt": endDate,
        },
     "$where":bson.M{"device_alias":bson.M{"$in": aliases}}
        
    }

请有人帮我解决这个问题

您的尝试不是有效的 mongodb 查询。试试这个:

query := bson.M{
        "datetime": bson.M{
            "$gt": startDate,
            "$lt": endDate,
        },
        "device_alias":bson.M{"$in": aliases},
    }