C# MongoDB – 过滤嵌套数组数据

huangapple go评论112阅读模式
英文:

C# MongoDB - Filtering nested array data

问题

我是MongoDB的新手,正在尝试在C#上使用它。假设我有这样的文档:

  1. [
  2. {
  3. "Number": "2140007529",
  4. "Name": "ABC",
  5. "IsInactive": true,
  6. "EntryList": [
  7. {
  8. "Timestamp": "2022-06-01T14:00:00.000+00:00",
  9. "Value": 21564.0
  10. },
  11. {
  12. "Timestamp": "2022-07-01T21:31:00.000+00:00",
  13. "Value": 21568.0
  14. },
  15. {
  16. "Timestamp": "2022-08-02T21:21:00.000+00:00",
  17. "Value": 21581.642
  18. },
  19. {
  20. "Timestamp": "2022-09-02T15:42:00.000+00:00",
  21. "Value": 21593.551
  22. },
  23. {
  24. "Timestamp": "2022-09-26T13:00:00.000+00:00",
  25. "Value": 21603
  26. }
  27. ]
  28. },
  29. {
  30. "Number": "2220000784",
  31. "Name": "XYZ",
  32. "IsInactive": false,
  33. "EntryList": [
  34. {
  35. "Timestamp": "2022-09-26T13:00:00.000+00:00",
  36. "Value": 0.0
  37. },
  38. {
  39. "Timestamp": "2022-10-01T08:49:00.000+00:00",
  40. "Value": 5.274
  41. },
  42. {
  43. "Timestamp": "2022-11-01T09:56:00.000+00:00",
  44. "Value": 76.753
  45. },
  46. {
  47. "Timestamp": "2022-12-01T19:43:00.000+00:00",
  48. "Value": 244.877
  49. },
  50. {
  51. "Timestamp": "2023-01-01T11:54:00.000+00:00",
  52. "Value": 528.56
  53. },
  54. {
  55. "Timestamp": "2023-02-01T17:21:00.000+00:00",
  56. "Value": 802.264
  57. }
  58. ]
  59. }
  60. ]

我想要获取IsInactive标志为false的文档,但对于EntryList,只返回大于2022-12-31的时间戳。结果应该如下所示:

  1. {
  2. "Number": "2220000784",
  3. "Name": "XYZ",
  4. "IsInactive": false,
  5. "EntryList": [
  6. {
  7. "Timestamp": "2023-01-01T11:54:00.000+00:00",
  8. "Value": 528.56
  9. },
  10. {
  11. "Timestamp": "2023-02-01T17:21:00.000+00:00",
  12. "Value": 802.264
  13. }
  14. }
  15. }

所以,这是我的问题:如何在C#中筛选返回值中的嵌套数组。感谢您的帮助!

我尝试使用MongoDB Compass的聚合功能来获取结果,它在MongoDB Compass中可以工作,但在C#中不行。

英文:

i am new on MongoDB and i am trying to use it in C# context. Let´s say, i have documents like this:

  1. [
  2. {
  3. "Number": "2140007529",
  4. "Name": "ABC",
  5. "IsInactive": true,
  6. "EntryList": [
  7. {
  8. "Timestamp": "2022-06-01T14:00:00.000+00:00",
  9. "Value": 21564.0
  10. },
  11. {
  12. "Timestamp": "2022-07-01T21:31:00.000+00:00",
  13. "Value": 21568.0
  14. },
  15. {
  16. "Timestamp": "2022-08-02T21:21:00.000+00:00",
  17. "Value": 21581.642
  18. },
  19. {
  20. "Timestamp": "2022-09-02T15:42:00.000+00:00",
  21. "Value": 21593.551
  22. },
  23. {
  24. "Timestamp": "2022-09-26T13:00:00.000+00:00",
  25. "Value": 21603
  26. }
  27. ]
  28. },
  29. {
  30. "Number": "2220000784",
  31. "Name": "XYZ",
  32. "IsInactive": false,
  33. "EntryList": [
  34. {
  35. "Timestamp": "2022-09-26T13:00:00.000+00:00",
  36. "Value": 0.0
  37. },
  38. {
  39. "Timestamp": "2022-10-01T08:49:00.000+00:00",
  40. "Value": 5.274
  41. },
  42. {
  43. "Timestamp": "2022-11-01T09:56:00.000+00:00",
  44. "Value": 76.753
  45. },
  46. {
  47. "Timestamp": "2022-12-01T19:43:00.000+00:00",
  48. "Value": 244.877
  49. },
  50. {
  51. "Timestamp": "2023-01-01T11:54:00.000+00:00",
  52. "Value": 528.56
  53. },
  54. {
  55. "Timestamp": "2023-02-01T17:21:00.000+00:00",
  56. "Value": 802.264
  57. }
  58. ]
  59. }
  60. ]

I want to get the document where the IsInactive flag is false. But for the EntryList there should be returned entries greater than Timestamp 2022-12-31 only.I should look like this:

  1. {
  2. "Number": "2220000784",
  3. "Name": "XYZ",
  4. "IsInactive": false,
  5. "EntryList": [
  6. {
  7. "Timestamp": "2023-01-01T11:54:00.000+00:00",
  8. "Value": 528.56
  9. },
  10. {
  11. "Timestamp": "2023-02-01T17:21:00.000+00:00",
  12. "Value": 802.264
  13. }
  14. ]
  15. }

So, here is my question. How can i filter nested arrays in return value with C#. Thanks for help!

I tried to get the result with aggregate function of MongoDB in MongoDB Compass. I got it work with but not in C#.

答案1

得分: 0

我认为您正在寻找类似于此查询的查询。因此,您可以尝试类似于以下代码的内容:

  1. var desiredTimestamp = new DateTime(2022, 12, 31);
  2. var results = collection.AsQueryable()
  3. .Where(x => x.IsInactive == false && x.EntryList.Any(e => e.Timestamp >= desiredTimestamp))
  4. .Select(obj => new
  5. {
  6. Number = obj.Number,
  7. Name = obj.Name,
  8. IsInactive = obj.IsInactive,
  9. EntryList = obj.EntryList
  10. .Where(e => e.Timestamp >= desiredTimestamp)
  11. .ToList()
  12. }).ToList()

请注意,我假设您的 Timestamp 是日期类型,否则您无法比较日期和字符串。

英文:

I think you are looking for a query similar to this one.

So you can try something like this code:

  1. var desiredTimestamp = new DateTime(2022, 12, 31);
  2. var results = collection.AsQueryable()
  3. .Where(x => x.IsInactive == false && x.EntryList.Any(e => e.Timestamp >= desiredTimestamp))
  4. .Select(obj => new
  5. {
  6. Number = obj.Number,
  7. Name = obj.Name,
  8. IsInactive = obj.IsInactive,
  9. EntryList = obj.EntryList
  10. .Where(e => e.Timestamp >= desiredTimestamp)
  11. .ToList()
  12. }).ToList()

Note that I'm assuming your Timestamp is a date type, otherwise you can't compare date and string.

huangapple
  • 本文由 发表于 2023年2月6日 06:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定