Query to find the data in specific Time Range

Hello Everyone, My datetime is in measurement collection and my temperature value is in temperature value collection. I Need to find out at particular time period (ex - 09:00 am - 10:00 am) how many temperature values were recorded. I am using C# fro this.

Here is my measurement class:

using MongoDB.Entities;
using MongoDB.Bson;

 public class Measurement : Entity
    {
        [BsonElement("daterecorded")]
        public DateTime DateRecorded { get; set; }
        public Many<TemperatureValues> TValues { get; set; }
        public Measurement()
        {
            this.InitOneToMany(() => TValues);
        }

    }

This is TemperatureValue Class

 using MongoDB.Entities;
 using MongoDB.Bson;

 public class TemperatureValues : Entity
    {
        [BsonElement("tempsensorcount")]
        public int TempSensorCount { get; set; }

        [BsonElement("values")]
        public short Values { get; set; }

    }

Here is the data on my Measurement collection;

    {"_id":{"$oid":"614981adba6b193698c931b3"},"daterecorded":{"$date":"2021-09-21T06:54:36.864Z"}}

    {"_id":{"$oid":"614981adba6b193698c931b3"},"daterecorded":{"$date":"2021-09-21T06:54:36.864Z"}}

Here is the data on my TemperatureValue colllection;

{"_id":{"$oid":"614981adba6b193698c931c4"},"tempsensorcount":0,"values":0}

{"_id":{"$oid":"614981adba6b193698c931d6"},"tempsensorcount":0,"values":0}

Here is the auto generated Parent Child Id because of using Entity:

{"_id":{"$oid":"614981ad374a99f37fda0f14"},"ChildID":{"$oid":"614981adba6b193698c931c4"},"ParentID":{"$oid":"614981adba6b193698c931b3"}}

`{"_id":{"$oid":"614981ad374a99f37fda0f48"},"ChildID":{"$oid":"614981adba6b193698c931d6"},"ParentID":{"$oid":"614981adba6b193698c931c5"}}`

I tried this Query:

Measurement ms = new Measurement();
 var TemperaturDetails = await ( 
from j in ms.TValues.JoinQueryable() 
where j.ChildID ==Dateime("2021-09-20T11:44:39.829+00:00); join b in DB.Collection<Measurement>() on j.ParentID equals [
b.ID select b ).ToListAsync();

 Console.WriteLine($"temp : {TemperaturDetails.Count()}");

I believe this is the wrong approach:

May Be I need to write 2 queryies for this, In first I need to filter all the data in Measuremnet collection with time range. And in second query need to filter the count of temperature value in the time range.

Please do help me in writing the query.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.