gpt4 book ai didi

mongodb - 获取日期范围内的数组元素

转载 作者:可可西里 更新时间:2023-11-01 10:31:23 24 4
gpt4 key购买 nike

这是我的 MongoDB 集合中的示例文档:

 var user: [{
"_id" : ObjectId("5391b11a3c0a9ac01300006d"),
"injurydata" : {
"injuryinformation" : [
{
"status" : "current",
"updateddate" : ISODate("2014-06-06T12:16:20.306Z"),
"updatedby" : "",
"dateofinjury" : ISODate("2014-06-27T18:30:00.000Z"),
"_id" : ObjectId("5391b11a3c0a9ac01300006f")
}
]
}
},
{
"_id" : ObjectId("5391b11a3c0a9ac01300006d"),
"injurydata" : {
"injuryinformation" : [
{
"status" : "current",
"updateddate" : ISODate("2014-06-06T12:16:20.306Z"),
"dateofinjury" : ISODate("2014-06-28T18:30:00.000Z"),
"_id" : ObjectId("5391b11a3c0a9ac01300006f")
}
]
}
},
{
"_id" : ObjectId("5391b11a3c0a9ac01300006d"),
"injurydata" : {
"injuryinformation" : [
{
"status" : "current",
"dateofinjury" : ISODate("2014-08-10T18:30:00.000Z"),
"_id" : ObjectId("5391b11a3c0a9ac01300006f")
}
]
}
}]

从这份文件中,我只想要 dateofinJURY 在 2014-06-28 和 2014-06-30 之间的那些 injuryinformation 数组元素,所以我只会得到前两个元素。

我尝试了这个查询,但我仍然得到了整个数组

db.user.find(
{
$and:
[
{"injury.injurydata.injuryinformation.dateofinjury":
{"$gte": ISODate("2014-05-21T08:00:00Z") , "$lt": ISODate("2014-06- 03T08:00:00Z")}},
{"_id":ObjectId("538d9a1dd173e5202a00005d")}
],

})

最佳答案

好吧,_id 匹配在您的语句中相当明确,因此这不仅会否定对数组条目进行“范围”匹配的需要,而且还会否定 $and 的任何用法。这实际上隐含在 MongoDB 查询中。 $and condition 本质上是“默认”并且不是必需的,除非您实际上在同一字段上要求多个条件。但你不在这里。

匹配多个数组元素需要用到.aggregate()因为可用于 .find() 的“投影”无法向您显示数组中的多个匹配条目:

db.collection.aggregate([

// Match the document but not actually the array
{ "$match": {
"injury.injurydata.injuryinformation.dateofinjury": {
"$gte": ISODate("2014-05-21T08:00:00Z"),
"$lt": ISODate("2014-06-03T08:00:00Z")
},
"_id": ObjectId("538d9a1dd173e5202a00005d")
}},

// Unwind the arrays to "de-normalize" as documents
{ "$unwind": "$injury" },
{ "$unwind": "$injury.injurydata" },
{ "$unwind": "$injury.injurydata.injuryinformation" },

// Match the actual array elements
{ "$match": {
"injury.injurydata.injuryinformation.dateofinjury": {
"$gte": ISODate("2014-05-21T08:00:00Z"),
"$lt": ISODate("2014-06-03T08:00:00Z")
}
}},

// Group those back to and array, maybe?
{ "$group": {
"_id": "$_id",
"information": {
"$push": "$injury.injrydata.injuryinformation"
}
}}
])

或使用 $map 进行“过滤”在 MongoDB 2.6 及更高版本下,看起来更长但更快:

db.collection.aggregate([

// Match the document but not actually the array
{ "$match": {
"injury.injurydata.injuryinformation.dateofinjury": {
"$gte": ISODate("2014-05-21T08:00:00Z"),
"$lt": ISODate("2014-06-03T08:00:00Z")
},
"_id": ObjectId("538d9a1dd173e5202a00005d")
}},

// Unwind the arrays to "de-normalize" as documents
{ "$unwind": "$injury" },
{ "$unwind": "$injury.injurydata" },

// Project with the "$map" filter
{ "$project": {
"information": {
"$setDifference": [
"$map": {
"input": "$injury.injurydata.injuryinformation",
"as": "el",
"in": {
"$cond": [
{
"$and": [
{
"$gte": [
"$$el.dateofinjury",
ISODate("2014-05-21T08:00:00Z")
]
},
{
"$lt": [
"$$el.dateofinjury",
ISODate("2014-06-03T08:00:00Z")
]
}
]
}
],
false
}
},
[false]
]
}
}}

])

那里使用的 $and 条件是一种不同的形式,可用于 $match 操作之外的聚合框架表达式是等效的.find() 查询。

不确定“伤害”顶线从何而来,因为它没有出现在你的样本中,但我假设你确实有它。

关于mongodb - 获取日期范围内的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24082174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com