How to query / update supporting document in MongoDB using C # driver

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 the following questions (using the official C # driver):

  • How to get one HourData document from the DayData collection (using one database query)? for example I need to get a HourData document for HR1 for DayData (where _id = "2012_11_10"). Please refer to the code snippet that I used as Edit-1.

  • How to update / update a HourData document to increase its number (using a single database operation, for example: collection.update (Query, Update))? For example, I need to increase the HR1 score for DayData (where _id = "2012_11_10").

  • How to get the sum of all Count values โ€‹โ€‹HR1, HR2, ..., HR24 for DayData (where _id = "2012_11_10") (using some aggregate function).

  • What is the best way to convert the Count Hour Count of all hours to an array (for any DayData). for example for DayData with _id = "2012_11_10", I need:

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

Edit-1

With this code, I intend to get HourData for HR1, where its _id = 1 and DayData with _id = "2012_11_10", but it returns nothing.

 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
source share
1 answer

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 simply extract an instance of DayData and then summarize all the necessary 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 is not elegant. If you want an elegant solution, you need to convert your fields to an array, for example:

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

and work like an array. Let me know if it is possible to convert it to an array.

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

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

Or you can create an enumerator directly in the class, but I think this is overkill in your case.

+2
source

Source: https://habr.com/ru/post/1445934/


All Articles