Javascript 按日期或日期范围进行查询
我目前能够成功地从数据库中获取数据,创建索引,然后在react.js渐进式web应用程序中使用PockDB获取最近的7条记录 我目前的障碍是按日期查询。因此,在我当前的代码中,我获取所有数据,创建一个索引,然后只获取7条最新记录,以便映射数据。但我正试图找出如何使用我的当前日期的时刻:Javascript 按日期或日期范围进行查询,javascript,reactjs,pouchdb,Javascript,Reactjs,Pouchdb,我目前能够成功地从数据库中获取数据,创建索引,然后在react.js渐进式web应用程序中使用PockDB获取最近的7条记录 我目前的障碍是按日期查询。因此,在我当前的代码中,我获取所有数据,创建一个索引,然后只获取7条最新记录,以便映射数据。但我正试图找出如何使用我的当前日期的时刻:newestDate:moment(this.currentDate).format(“YYYY-MM-DD”) 并用它来表示“获取过去7天内每天的最新记录” 因此,有效地说,我有格式为{caroriesburne
newestDate:moment(this.currentDate).format(“YYYY-MM-DD”)
并用它来表示“获取过去7天内每天的最新记录”
因此,有效地说,我有格式为{caroriesburned:“250”,caroriesconsumed:“1450”,createdAt:“2020-03-05”}
的记录,我希望能够修改我的查询,从今天开始,每天只获取一条记录,并持续7天
如何使用以下代码在PockDB中正确执行此操作:
fetchData = () => {
this.setState({
calorieElements: null,
});
this.state.caloriesDB.db.allDocs({
include_docs: true,
}).then(result => {
const rows = result.rows;
console.log('this is a row');
console.log(result);
this.setState({
calorieElements: rows.map(row => row.doc),
});
console.log(this.state.calorieElements);
}).catch((err) =>{
console.log(err);
});
}
getMax = () => {
this.state.caloriesDB.db.createIndex({
index: {
fields: ['_id','caloriesBurned', 'createdAt']
}
}).then(result => {
console.log(result);
this.setMax();
}).catch((err) =>{
console.log(err);
});
}
setMax = () => {
this.state.caloriesDB.db.find({
selector: {
$and: [
{_id: {"$gte": null}},
{caloriesBurned: {$exists: true}},
{caloriesConsumed: {$exists: true}},
{createdAt: {$exists: true}}
]
},
fields: ['caloriesBurned','caloriesConsumed', 'createdAt'],
sort: [{'_id':'desc'}],
limit: 7
}).then(result => {
const newDocs = result.docs;
const docCalories = newDocs.map(x => +x.caloriesConsumed - +x.caloriesBurned);
}).catch((err) =>{
console.log(err);
});
}
这里有一个例子,说明了如何实现你的愿望。我已经尽可能地把代码保存在你的附近了。关键是索引字段的顺序,首先是
createdAt
以便于排序
let db;
//演示
initDb()。然后(()=>{
//在2020-03-09获得最后7天
返回queryCreatedAt(“2020-03-09”,7,“描述”);
})。然后(结果=>{
//显示结果。
控制台日志(结果);
})
//通过createdAt查询文档
函数queryCreatedAt(日期、限制、顺序){
返回db.find({
选择器:{
美元及:[{
创建数据:{
$lte:日期
}
},
{
燃烧的卡路里:{
$exists:true
}
},
{
消耗的热量:{
$exists:true
}
}
]
},
排序:[{
createdAt:order
}],
极限:极限,
字段:[
“createdAt”,
“燃烧的卡路里”,
“消耗的热量”
]
});
}
//初始化数据库实例
函数initDb(){
db=新的db(“测试”{
适配器:“内存”
});
返回db.bulkDocs(getDocsToInstall())。然后(()=>{
返回db.createIndex({
索引:{
字段:[
“createdAt”,
“燃烧的卡路里”,
“消耗的热量”
]
}
})
})
}
//获取测试文档
函数getDocsToInstall(){
返回[{
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“createdAt”:“2020-02-27”
}, {
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“createdAt”:“2020-02-28”
}, {
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“createdAt”:“2020-02-29”
},
{
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“创建数据”:“2020-03-01”
}, {
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“创建数据”:“2020-03-02”
},
{
“燃烧卡路里”:“950”,
“消耗热量”:“1950年”,
“createdAt”:“2020-03-03”
},
{
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“创建日期”:“2020-03-04”
},
{
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“创建日期”:“2020-03-05”
},
{
“燃烧卡路里”:“350”,
“消耗热量”:“1350”,
“createdAt”:“2020-03-06”
},
{
“燃烧卡路里”:“450”,
“消耗热量”:“1450”,
“createdAt”:“2020-03-07”
},
{
“燃烧卡路里”:“550”,
“消耗热量”:“1550”,
“创建日期”:“2020-03-08”
}
]
}
澄清-每天只有一条记录,对吗?我希望有一个每天有多条记录的解决方案(在这种情况下获取当天的最后一条记录),但我会接受每天有一条记录的解决方案,因为我可以更新当天的记录