如何查询mongo中的日期范围,其中字段本身是一个日期
How to query date range in mongo, where field itself is a date
我有一个 JSON 文档存储在 mongodb 中,其中一个子文档是日期时间戳。我需要查询和过滤日期范围内的子文档。
我正在使用 mongo shell 来处理查询。
{
"_id": ObjectID("5d9bf09c242af456ff5dd149"),
"configId": "c2",
"name": "ajit test",
"description": "this is test desc",
"confidence": 0,
"report": {
"2019-10-05T02:12:44Z": [
{
"VariantId": "1",
"bestProbability": "3.2",
"hdi": {
"low": "2.1",
"high": "4.0"
}
},
{
"VariantId": "2",
"bestProbability": "3.2",
"hdi": {
"low": "4.5",
"high": "4.7"
}
}
],
"2019-10-06T02:12:44Z": [
{
"VariantId": "1",
"bestProbability": "3.2",
"hdi": {
"low": "2.1",
"high": "4.0"
}
},
{
"VariantId": "2",
"bestProbability": "3.2",
"hdi": {
"low": "4.5",
"high": "4.7"
}
}
],
"2019-10-08T02:12:44Z": [
{
"VariantId": "1",
"bestProbability": "3.2",
"hdi": {
"low": "3.5",
"high": "6.7"
}
},
{
"VariantId": "2",
"bestProbability": "3.2",
"hdi": {
"low": "3.5",
"high": "6.7"
}
}
]
},
}
我正在寻找一个查询,该查询必须 return 日期为 2019-10-06T02:12:44Z 和 2019-10-08T02:12:44Z 之间的嵌入文档的子集。
我们需要将子文档report
转换为一个键值对数组,其中键代表日期。稍后,该数组必须被过滤并再次转换回对象。
以下查询可以获得预期的输出:
db.collection.aggregate([
{
$addFields:{
"report":{
$objectToArray:"$report"
}
}
},
{
$addFields:{
"report":{
$filter:{
"input":"$report",
"as":"doc",
"cond":{
$and:[
{
$gte:["$$doc.k","2019-10-06T02:12:44Z"]
},
{
$lte:["$$doc.k","2019-10-08T02:12:44Z"]
}
]
}
}
}
}
},
{
$addFields:{
"report":{
$arrayToObject:"$report"
}
}
}
]).pretty()
数据集:
{
"_id" : ObjectId("5d9c15312cb9ef5d628ea95d"),
"configId" : "c2",
"name" : "ajit test",
"description" : "this is test desc",
"confidence" : 0,
"report" : {
"2019-10-05T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-06T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-08T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
}
]
}
}
输出:
{
"_id" : ObjectId("5d9c15312cb9ef5d628ea95d"),
"configId" : "c2",
"name" : "ajit test",
"description" : "this is test desc",
"confidence" : 0,
"report" : {
"2019-10-06T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-08T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
}
]
}
}
我有一个 JSON 文档存储在 mongodb 中,其中一个子文档是日期时间戳。我需要查询和过滤日期范围内的子文档。
我正在使用 mongo shell 来处理查询。
{
"_id": ObjectID("5d9bf09c242af456ff5dd149"),
"configId": "c2",
"name": "ajit test",
"description": "this is test desc",
"confidence": 0,
"report": {
"2019-10-05T02:12:44Z": [
{
"VariantId": "1",
"bestProbability": "3.2",
"hdi": {
"low": "2.1",
"high": "4.0"
}
},
{
"VariantId": "2",
"bestProbability": "3.2",
"hdi": {
"low": "4.5",
"high": "4.7"
}
}
],
"2019-10-06T02:12:44Z": [
{
"VariantId": "1",
"bestProbability": "3.2",
"hdi": {
"low": "2.1",
"high": "4.0"
}
},
{
"VariantId": "2",
"bestProbability": "3.2",
"hdi": {
"low": "4.5",
"high": "4.7"
}
}
],
"2019-10-08T02:12:44Z": [
{
"VariantId": "1",
"bestProbability": "3.2",
"hdi": {
"low": "3.5",
"high": "6.7"
}
},
{
"VariantId": "2",
"bestProbability": "3.2",
"hdi": {
"low": "3.5",
"high": "6.7"
}
}
]
},
}
我正在寻找一个查询,该查询必须 return 日期为 2019-10-06T02:12:44Z 和 2019-10-08T02:12:44Z 之间的嵌入文档的子集。
我们需要将子文档report
转换为一个键值对数组,其中键代表日期。稍后,该数组必须被过滤并再次转换回对象。
以下查询可以获得预期的输出:
db.collection.aggregate([
{
$addFields:{
"report":{
$objectToArray:"$report"
}
}
},
{
$addFields:{
"report":{
$filter:{
"input":"$report",
"as":"doc",
"cond":{
$and:[
{
$gte:["$$doc.k","2019-10-06T02:12:44Z"]
},
{
$lte:["$$doc.k","2019-10-08T02:12:44Z"]
}
]
}
}
}
}
},
{
$addFields:{
"report":{
$arrayToObject:"$report"
}
}
}
]).pretty()
数据集:
{
"_id" : ObjectId("5d9c15312cb9ef5d628ea95d"),
"configId" : "c2",
"name" : "ajit test",
"description" : "this is test desc",
"confidence" : 0,
"report" : {
"2019-10-05T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-06T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-08T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
}
]
}
}
输出:
{
"_id" : ObjectId("5d9c15312cb9ef5d628ea95d"),
"configId" : "c2",
"name" : "ajit test",
"description" : "this is test desc",
"confidence" : 0,
"report" : {
"2019-10-06T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "2.1",
"high" : "4.0"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "4.5",
"high" : "4.7"
}
}
],
"2019-10-08T02:12:44Z" : [
{
"VariantId" : "1",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
},
{
"VariantId" : "2",
"bestProbability" : "3.2",
"hdi" : {
"low" : "3.5",
"high" : "6.7"
}
}
]
}
}