Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:30:24+00:00 2026-06-14T15:30:24+00:00

public class DayData { public string _id {get;set;} public string Data {get;set;} public HourData

  • 0
public class DayData
{
    public string _id
    {get;set;}

    public string Data
    {get;set;}

    public HourData HR1
    {get;set;}

    public HourData HR2
    {get;set;}

    ...

    public HourData HR24
    {get;set;}
 }



public class HourData
{
    public long _id
    {get;set;}

    public int Count
    {get;set;}

    public string Data
    {get;set;}
}

// Sample Data

{ 
    "_id": "2012_11_10", 
    "Data": "some data", 
    "HR1": 
    { 
        "_id": 1 
        "Count": 100, 
        "Data": "Hour 1 Data" 
    },
    "HR2": 
    { 
        "_id": 2 
        "Count": 200, 
        "Data": "Hour 2 Data" 
    },

    ...

    "HR24": 
    { 
        "_id": 24 
        "Count": 2400, 
        "Data": "Hour 24 Data" 
    }
}

I have following questions (by using C# official driver):

  1. How to retrieve single HourData document from DayData collection (using single database query)? e.g. I need to retrieve HourData document of HR1 for DayData (where _id=”2012_11_10″). Please refer to code snippet i tried as Edit-1.

  2. How to update/upsert HourData document to increment its Count (using single database operation, like: collection.update(Query, Update))? e.g. I need to increment Count of HR1 for DayData (where _id=”2012_11_10″).

  3. How to retrieve Sum of all Count values of HR1, HR2,…,HR24 for DayData (where _id=”2012_11_10″) (using some aggregate function).

  4. What is the best way to convert the HourData Counts of all hours to an array (for any DayData). e.g. for a DayData with _id=”2012_11_10″, i need:

    int []Counts = [100,200,300, … , 2400]

Edit-1

With this code I intend to get HourData of HR1 where its _id=1 and DayData with _id=”2012_11_10″, but it does not return anything.

MongoCollection<HourData> dayInfo = mdb.GetCollection<HourData>("HourData");
var query = Query.And(Query.EQ("_id", "2012_11_10"), Query.EQ("HR1._id", 1));
MongoCursor<HourData> hri = dayInfo.Find(query);'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:30:25+00:00Added an answer on June 14, 2026 at 3:30 pm

    1)

    QueryComplete = Query.EQ(_id, "2012_11_10");
    DayData myData = db.GetCollection<DayData>("DayDataCollection").FindOne(query);
    // As HourData is the class member you can retrieve it from the instance of the DayData:
    HourData myHour = myData.HR1;
    

    2)

    QueryComplete = Query.EQ(_id, "2012_11_10");
    UpdateBuilder update = Update.Inc("HR1.Count", 1);
    db.GetCollection<DayData>("DayDataCollection").Update(query, update, SafeMode.True)
    

    ;

    3)
    In your case you just retrieve the DayData instance and then sum all needed values explicitly:

    QueryComplete = Query.EQ(_id, "2012_11_10");
    DayData myData = db.GetCollection<DayData>("DayDataCollection").FindOne(query);
    // As HourData is the class member you can retrieve it from the instance of the DayData:
    int sum = myData.HR1.Count + myData.HR2.Count + ... + myData.HR24.Count;
    

    But it’s not elegant. If you want the elegant solution you need to transform your fields into the array like:

    DayData
    {
    HR:
    [{
    Count: 1,
    Data: "Hour 1 data"
    },
    {
    },
    ...
    ]
    }
    

    and work as with the array. Let me know if it’s possible to transform it into an array.

    4)
    In your case, again, there is no any elegant solution. What you can do just go through your fields and create an array:

    int[] Counts = new int[24];
    Counts[0] = myData.HR1.Count;
    ...
    

    Or you can create enumerator right in the class but I think it’s overkill in your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Emp { public int ID { get; set; } public string Name
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class User { public long Id {get;set;} [References(typeof(City))] public long CityId {get;set;} [????]
public class Parent { public virtual long Id { get; set; } public virtual
public class Store { public virtual prop1 { get; set;} public virtual int RegionID
public class NewCallInfo { public string Site { get; set; } public string CustomerName
public class Address{ public string ContactName {get; private set;} public string Company {get; private
public class Abbreviation { public string ShortName { get; set; } public string LongName
public class MyEntity { public string Att1 { get; set; } public DateTime Att2
public class Item { public List<int> val { get; set; } public double support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.