我有一个股票收集与以下给定格式的多个文件,在mongo。
{
Id: ‘RELI’
Date: ‘2018-10-13’
Price: 900
}我想写几个问题:
我对前2期有书面询问。
1. db.stocks.find({
"id":"REL1",
"price":{
"$gte": ISODate(d1),
"$lt": ISODate(d2)
}
})
2.// Calculate the today’s date
let d1 = new Date()
// n is the number of months for which we are trying to fetch the
data
Let d2 = d1.setMonth(d.getMonth() - n);
db.stocks.find({
"id":"REL1",
"price":{
"$gte": ISODate(d1),
"$lt": ISODate(d2)
}
})你们能帮我做第三次和第四次吗?或者帮助我优化我写的前两个问题。
发布于 2018-10-13 12:27:08
对于去年的股票,您可以尝试以下查询
db.collection.find({
"id": "REL1",
"$expr": {
"$eq": [
{ "$year": {
"$dateFromString": { "dateString": "$Date" }
}},
2018
]
}
})对于d1和d2之间的股票,您的第一个查询是完美的吗?
https://stackoverflow.com/questions/52792554
复制相似问题