Mongodb 如何在mongo中查询日期范围,其中字段本身是日期
我在mongodb中存储了一个JSON文档,其中一个子文档是日期时间戳。我需要查询和筛选日期范围之间的子文档 我正在使用MongoShell处理查询Mongodb 如何在mongo中查询日期范围,其中字段本身是日期,mongodb,mongodb-query,mongo-shell,range-query,Mongodb,Mongodb Query,Mongo Shell,Range Query,我在mongodb中存储了一个JSON文档,其中一个子文档是日期时间戳。我需要查询和筛选日期范围之间的子文档 我正在使用MongoShell处理查询 { "_id": ObjectID("5d9bf09c242af456ff5dd149"), "configId": "c2", "name": "ajit test", "description": "this is test desc", "confidence": 0, "report": {
{
"_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"
}
}
]
},
}
我正在寻找一个查询,该查询必须仅返回日期2019-10-06T02:12:44Z和2019-10-08T02:12:44Z之间的嵌入式文档子集。我们需要将子文档
报告
转换为一个键值对数组,其中键表示日期。之后,必须对该数组进行过滤并再次将其转换回对象
以下查询可以获得预期的输出:
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"
}
}
]
}
}
数据集:
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"
}
}
]
}
}
输出:
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"
}
}
]
}
}
你能发布你的样本输出吗