gpt4 book ai didi

node.js - 如何使用 $filter 获取数组字段的过滤元素

转载 作者:太空宇宙 更新时间:2023-11-04 00:20:36 25 4
gpt4 key购买 nike

我在 MongoDB 中有一个火车集合,其中包含以下行:

{
"_id" : ObjectId("594a441fcbd8f815dc42436d"),
"trainnum" : "011Ф",
"startplace" : "Tashkent",
"destination" : "Samarkand",
"weeks" : [
"1",
"2",
"3"
],
"vagons" : [
{
"_id" : ObjectId("594a6a42231e5912342ff903"),
"numvag" : "01K",
"seatcount" : "30",
"reserves" : [
{
"numseats" : "1-10",
"numreserve" : "1",
"_id" : ObjectId("594a6a42231e5912342ff904")
}
],
"date2" : ISODate("2017-06-30T00:00:00Z"),
"date1" : ISODate("2017-06-05T00:00:00Z")
},
{
"_id" : ObjectId("594a6c0d231e5912342ff905"),
"numvag" : "02C",
"seatcount" : "01-10",
"reserves" : [
{
"numseats" : "01-10",
"numreserve" : "05",
"_id" : ObjectId("594a6c0d231e5912342ff906")
}
],
"date2" : ISODate("2017-06-21T00:00:00Z"),
"date1" : ISODate("2017-06-25T00:00:00Z")
}
],
"date2" : ISODate("2017-07-30T00:00:00Z"),
"date1" : ISODate("2017-06-01T00:00:00Z"),
"__v" : 0
}
{
"_id" : ObjectId("594a69c4231e5912342ff900"),
"trainnum" : "012A",
"startplace" : "Tashkent",
"destination" : "Samarkand",
"updated_at" : ISODate("2017-06-21T12:42:44.893Z"),
"weeks" : [
"1",
"2",
"3",
"4"
],
"vagons" : [
{
"_id" : ObjectId("594b54fe0925d110180abf7b"),
"numvag" : "01K",
"seatcount" : "30",
"reserves" : [ ],
"date2" : ISODate("2017-07-23T00:00:00Z"),
"date1" : ISODate("2017-06-30T00:00:00Z")
},
{
"_id" : ObjectId("594b55ede16d1908b8129b42"),
"numvag" : "02L",
"seatcount" : "40",
"reserves" : [ ],
"date2" : ISODate("2017-07-11T00:00:00Z"),
"date1" : ISODate("2017-06-29T00:00:00Z")
}
],
"date2" : ISODate("2017-07-30T00:00:00Z"),
"date1" : ISODate("2017-06-01T00:00:00Z"),
"__v" : 0
}

如何检索包含日期 1 (vagons date1) > 当前日期 (22.06.2017) (Vagons.date1>当前日期) 的所有货车的火车。结果应该是这样的:

{
"_id" : ObjectId("594a441fcbd8f815dc42436d"),
"trainnum" : "011Ф",
"startplace" : "Tashkent",
"destination" : "Samarkand"
"weeks" : [
"1",
"2",
"3"
],
"vagons" : [
{
"_id" : ObjectId("594a6c0d231e5912342ff905"),
"numvag" : "02C",
"seatcount" : "01-10",
"reserves" : [
{
"numseats" : "01-10",
"numreserve" : "05",
"_id" : ObjectId("594a6c0d231e5912342ff906")
}
],
"date2" : ISODate("2017-06-21T00:00:00Z"),
"date1" : ISODate("2017-06-25T00:00:00Z")
}
],
"date2" : ISODate("2017-07-30T00:00:00Z"),
"date1" : ISODate("2017-06-01T00:00:00Z"),
"__v" : 0
}
`{
"_id" : ObjectId("594a69c4231e5912342ff900"),
"trainnum" : "012A",
"startplace" : "Tashkent",
"destination" : "Samarkand",
"updated_at" : ISODate("2017-06-21T12:42:44.893Z"),
"weeks" : [
"1",
"2",
"3",
"4"
],
"vagons" : [
{
"_id" : ObjectId("594b54fe0925d110180abf7b"),
"numvag" : "01K",
"seatcount" : "30",
"reserves" : [ ],
"date2" : ISODate("2017-07-23T00:00:00Z"),
"date1" : ISODate("2017-06-30T00:00:00Z")
},
{
"_id" : ObjectId("594b55ede16d1908b8129b42"),
"numvag" : "02L",
"seatcount" : "40",
"reserves" : [ ],
"date2" : ISODate("2017-07-11T00:00:00Z"),
"date1" : ISODate("2017-06-29T00:00:00Z")
}
],
"date2" : ISODate("2017-07-30T00:00:00Z"),
"date1" : ISODate("2017-06-01T00:00:00Z"),
"__v" : 0
}`

最佳答案

只需应用 $filter场上的情况。使用$gt对于“cond”参数。

如果您没有 MongoDB 3.4 或更高版本,请使用 $project而不是$addFields并简单地将所有其他字段包含在投影中:

Train.aggregate([
{ "$match": {
"vagons.date1": { "$gte": new Date("2017-06-22") }
}},
{ "$addFields": {
"vagons": {
"$filter": {
"input": "$vagons",
"as": "v",
"cond": { "$gt": [ "$$v.date1", new Date("2017-06-22") ] }
}
}
}}
])

给出输出:

{
"_id" : ObjectId("594a441fcbd8f815dc42436d"),
"trainnum" : "011Ф",
"startplace" : "Tashkent",
"destination" : "Samarkand",
"weeks" : [
"1",
"2",
"3"
],
"vagons" : [
{
"_id" : ObjectId("594a6c0d231e5912342ff905"),
"numvag" : "02C",
"seatcount" : "01-10",
"reserves" : [
{
"numseats" : "01-10",
"numreserve" : "05",
"_id" : ObjectId("594a6c0d231e5912342ff906")
}
],
"date2" : ISODate("2017-06-21T00:00:00Z"),
"date1" : ISODate("2017-06-25T00:00:00Z")
}
],
"date2" : ISODate("2017-07-30T00:00:00Z"),
"date1" : ISODate("2017-06-01T00:00:00Z"),
"__v" : 0
}
{
"_id" : ObjectId("594a69c4231e5912342ff900"),
"trainnum" : "012A",
"startplace" : "Tashkent",
"destination" : "Samarkand",
"updated_at" : ISODate("2017-06-21T12:42:44.893Z"),
"weeks" : [
"1",
"2",
"3",
"4"
],
"vagons" : [
{
"_id" : ObjectId("594b54fe0925d110180abf7b"),
"numvag" : "01K",
"seatcount" : "30",
"reserves" : [ ],
"date2" : ISODate("2017-07-23T00:00:00Z"),
"date1" : ISODate("2017-06-30T00:00:00Z")
},
{
"_id" : ObjectId("594b55ede16d1908b8129b42"),
"numvag" : "02L",
"seatcount" : "40",
"reserves" : [ ],
"date2" : ISODate("2017-07-11T00:00:00Z"),
"date1" : ISODate("2017-06-29T00:00:00Z")
}
],
"date2" : ISODate("2017-07-30T00:00:00Z"),
"date1" : ISODate("2017-06-01T00:00:00Z"),
"__v" : 0
}

关于node.js - 如何使用 $filter 获取数组字段的过滤元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44692694/

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