英文:
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的尝试,我发现很难构建查询:
aliases := []string{"000", "111"}
query := bson.M{
"datetime": bson.M{
"$gt": startDate,
"$lt": endDate,
},
"$where": bson.M{"device_alias": bson.M{"$in": aliases}},
}
请问有人可以帮助我解决这个问题吗?
英文:
I would like to get document entries based on time range and a field that is used in $in. I can do it very easily using mongoose and nodejs like so where param is the key and params is an array
Data.find(
{
timestamp:
{
$gte: new Date(query.startDate),
$lt: new Date(query.endDate)
}
}
).where(`${param}`).in(params);
Below is my attempt with bson using go and bson. I am finding it diffucult on how to structure the query
aliases := []string {"000","111"}
query := bson.M{
"datetime": bson.M{
"$gt": startDate,
"$lt": endDate,
},
"$where":bson.M{"device_alias":bson.M{"$in": aliases}}
}
Kindly could someone help me figure this out
答案1
得分: 0
你的尝试不是一个有效的MongoDB查询。请尝试使用以下代码替代:
query := bson.M{
"datetime": bson.M{
"$gt": startDate,
"$lt": endDate,
},
"device_alias": bson.M{"$in": aliases},
}
英文:
Your attempt is not a valid mongodb query. Try this instead:
query := bson.M{
"datetime": bson.M{
"$gt": startDate,
"$lt": endDate,
},
"device_alias":bson.M{"$in": aliases},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论