在MongoDB中的另一个数组的日期字符串阵列中应用日期过滤器
我有一个包含一个称为“字段”的对象数组的集合,我还有另一个称为“值”的字符串数组,该数组在“字段”数组中。
现在,我必须使用日期过滤,但是该值保持内部值数组。
请帮助我形成查询。
像下面的收集对象一样,
{
"_id" : ObjectId("62aeb8301ed12a14a8873df1"),
"Fields" : [
{
"FieldId" : "e8efd0b0-9d10-4584-bb11-5b24f189c03b",
"Value" : [
"test_123"
]
},
{
"FieldId" : "fa6745c2-b259-4a3b-8c6f-19eb78fbbbf5",
"Value" : [
"123"
]
},
{
"FieldId" : "2a1be5d0-8fb6-4b06-a253-55337bfe4bcd",
"Value" : []
},
{
"FieldId" : "eed12747-0923-4290-b09c-5a05107f5609",
"Value" : [
"234234234"
]
},
{
"FieldId" : "fe41d8fb-fa18-4fe5-b047-854403aa4d84",
"Value" : [
"Irrelevan"
]
},
{
"FieldId" : "93e46476-bf2e-44eb-ac73-134403220e9e",
"Value" : [
"test"
]
},
{
"FieldId" : "db434aca-8df3-4caf-bdd7-3ec23252c2c8",
"Value" : [
"2019-06-16T18:30:00.000Z"
]
},
{
"FieldId" : "00df903f-5d59-41c1-a3df-60eeafb77d10",
"Value" : [
"tewt"
]
},
{
"FieldId" : "e97d0386-cd42-6277-1207-e674c3268cec",
"Value" : [
"1"
]
},
{
"FieldId" : "35e55d27-7d2c-467d-8a88-09ad6c9f5631",
"Value" : [
"10"
]
}
]
}
我的查询如下,
db.getCollection('Table1').find({
$and:[
{FieldsDatas:{$elemMatch:{FieldId:'e8efd0b0-9d10-4584-bb11-5b24f189c03b',Value:{$regex:'^mani',$options:'i'}}}},
{FieldsDatas:{$elemMatch:{FieldId:'e97d0386-cd42-6277-1207-e674c3268cec',Value:{$in:['1','2','1,2']}}}},
{FieldsDatas:{$elemMatch:{FieldId:'35e55d27-7d2c-467d-8a88-09ad6c9f5631',Value:'10'}}},
{FieldsDatas:{$elemMatch:{FieldId:'db434aca-8df3-4caf-bdd7-3ec23252c2c8','$first':
{Value:$gt:[{$toDate:'$this'},Date('2022-06-16')]}
}}}
]})
您可以看到日期可在“ 2019-06-16T18:30:00.000Z”中可用。我们必须仅对该字段应用过滤器。
注意:值数组只有一个值
$ todate过滤器不起作用,并且日期过滤器也无法正常工作。请找到以下链接,其中包含样品,
htttps://mongoplay.net.net/p/sroayhwvig2
请建议我您的答案解决这个问题。
提前致谢, 玛尼
I am having a collection which holding array of objects which is called as "Fields" and I am having another string array called as "Value" which is inside "Fields" array.
Now I have to filter using date but the value is holding inside Value array.
Please help me to form the query.
The collection object like below,
{
"_id" : ObjectId("62aeb8301ed12a14a8873df1"),
"Fields" : [
{
"FieldId" : "e8efd0b0-9d10-4584-bb11-5b24f189c03b",
"Value" : [
"test_123"
]
},
{
"FieldId" : "fa6745c2-b259-4a3b-8c6f-19eb78fbbbf5",
"Value" : [
"123"
]
},
{
"FieldId" : "2a1be5d0-8fb6-4b06-a253-55337bfe4bcd",
"Value" : []
},
{
"FieldId" : "eed12747-0923-4290-b09c-5a05107f5609",
"Value" : [
"234234234"
]
},
{
"FieldId" : "fe41d8fb-fa18-4fe5-b047-854403aa4d84",
"Value" : [
"Irrelevan"
]
},
{
"FieldId" : "93e46476-bf2e-44eb-ac73-134403220e9e",
"Value" : [
"test"
]
},
{
"FieldId" : "db434aca-8df3-4caf-bdd7-3ec23252c2c8",
"Value" : [
"2019-06-16T18:30:00.000Z"
]
},
{
"FieldId" : "00df903f-5d59-41c1-a3df-60eeafb77d10",
"Value" : [
"tewt"
]
},
{
"FieldId" : "e97d0386-cd42-6277-1207-e674c3268cec",
"Value" : [
"1"
]
},
{
"FieldId" : "35e55d27-7d2c-467d-8a88-09ad6c9f5631",
"Value" : [
"10"
]
}
]
}
I am having query like,
db.getCollection('Table1').find({
$and:[
{FieldsDatas:{$elemMatch:{FieldId:'e8efd0b0-9d10-4584-bb11-5b24f189c03b',Value:{$regex:'^mani',$options:'i'}}}},
{FieldsDatas:{$elemMatch:{FieldId:'e97d0386-cd42-6277-1207-e674c3268cec',Value:{$in:['1','2','1,2']}}}},
{FieldsDatas:{$elemMatch:{FieldId:'35e55d27-7d2c-467d-8a88-09ad6c9f5631',Value:'10'}}},
{FieldsDatas:{$elemMatch:{FieldId:'db434aca-8df3-4caf-bdd7-3ec23252c2c8','$first':
{Value:$gt:[{$toDate:'$this'},Date('2022-06-16')]}
}}}
]})
you can see date is available inside Value array "2019-06-16T18:30:00.000Z". we have to apply filter for that field only.
Note : Value array have only one value
$toDate filter is not working and date filter also not working. Please find the below link where it has sample,
https://mongoplayground.net/p/sroaYHWViG2
Please suggest me your answers to resolve this.
Thanks in advance,
Mani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
$ MAP
和$ redy
从双嵌套数组中提取日期。使用$ convert
使其成为正确的日期对象,并使用onerror
选项使其他非日期值为null
。最后,再次使用$降低
再次提取唯一的日期对象,并在其上执行$ match
。这是 mongo playground 供您参考。
You can use
$map
and$reduce
to extract the date out from doubly nested array. Use$convert
to make it a proper date object and use theonError
option to make other non-date value to benull
. Finally use$reduce
again to extract the only date object again and perform$match
on it.Here is the Mongo Playground for your reference.