该查询应按月份过滤结果,似乎运行良好。但是我也不知道如何添加年份过滤器。
db.collection.find({
"$expr": {
"$eq": [{ "$month": "$timestamp" }, 12]
}
});
我试图提出类似的方法,但没有成功。
db.collection.find({
"$expr": {
"$and": [
{ "$eq": [{ "$month": "$timestamp" }, 12] },
{ "$eq": [{ "$year": "$timestamp" }, 2018] }
]
}
});
如何正确执行此操作?
答案 0 :(得分:0)
您可以使用aggregate
代替find
。
三步解决方案将非常有用:
$project
的年和月字段以及文档本身,使用$$ROOT
。由于$project
仅输出指定的字段,因此我们也需要投影文档本身。$match
按年份和月份过滤。$replaceRoot
将原始文档放回顶层(将其展平)。db.collection.aggregate([
{
"$project": {
"year": { "$year": "$timestamp" },
"month": { "$month": "$timestamp" },
"document": "$$ROOT"
}
},
{
"$match": {
"year": 2018,
"month": 12
}
},
{
"$replaceRoot": { "newRoot": "$document" }
}
])
答案 1 :(得分:0)
我想使用查找而不是聚合。只需添加另一个> <HTTParty::Response:0x7ff207cd0270 parsed_response=[{"id"=>69359, "name"=>"Sunken Secrets"}, {"id"=>69530, "name"=>"Dai Senryaku VII:
> Modern Military Tactics"}, {"id"=>81332, "name"=>"Stick Fighter II"},
> {"id"=>50649, "name"=>"Who Wants to be a Millionaire: 1st Edition"},
> {"id"=>22618, "name"=>"Battleground 7: Bull Run"}, {"id"=>104945,
> "name"=>"Woodpunk"}, {"id"=>91579, "name"=>"Racing Live"},
> {"id"=>22636, "name"=>"Call of Duty: Black Ops - Annihilation"},
> {"id"=>88041, "name"=>"Highway Wars"}, {"id"=>31258,
> "name"=>"Slymes"}, {"id"=>90512, "name"=>"Asian Riddles 4"},
> {"id"=>105842, "name"=>"Robots Vs Zombies: Transform To Race And
> Fight"}, {"id"=>85450, "name"=>"Transformers Prime: The Game"},
> {"id"=>104748, "name"=>"Space station - build your own ISS"},
> {"id"=>111593, "name"=>"Oppai Puzzle"}, {"id"=>42610,
> "name"=>"Hebereke's Popoon"}, {"id"=>105254, "name"=>"Codenames"},
> {"id"=>91756, "name"=>"Aban Hawkins & the 1000 SPIKES"}, {"id"=>2057,
> "name"=>"Out Run 2019"}, {"id"=>93987, "name"=>"Pi Story"}],
> @response=#<Net::HTTPOK 200 OK readbody=true>,
> @headers={"date"=>["Wed, 25 Dec 2019 18:04:48 GMT"],
> "content-type"=>["application/json;charset=utf-8"],
> "content-length"=>["1259"], "connection"=>["close"],
> "set-cookie"=>["__cfduid=de604d7fc295fb415e047830a8ce304281577297088;
> expires=Fri, 24-Jan-20 18:04:48 GMT; path=/; domain=.igdb.com;
> HttpOnly; SameSite=Lax"], "x-count"=>["124726"], "via"=>["1.1 vegur"],
> "cf-cache-status"=>["DYNAMIC"],
> "strict-transport-security"=>["max-age=15552000; preload"],
> "x-content-type-options"=>["nosniff"], "expect-ct"=>["max-age=604800,
> report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""], "server"=>["cloudflare"], "cf-ray"=>["54acbd52ae077cb2-MUC"]}>
即可为我工作。
$eq