如何使用C#驱动程序在MongoDB中查询/更新子文档 [英] How to query/update sub document in MongoDB using C# driver

查看:52
本文介绍了如何使用C#驱动程序在MongoDB中查询/更新子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;}
}

//样本数据

{ 
    "_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" 
    }
}

我有以下问题(使用C#官方驱动程序):

  1. 如何从DayData集合中检索单个HourData文档(使用单个数据库查询)?例如我需要为DayData检索HR1的HourData文档(其中_id ="2012_11_10").请参考我尝试过使用Edit-1的代码段.

  2. 如何更新/更新HourData文档以增加其计数(使用单个数据库操作,例如:collection.update(Query,Update))?例如我需要为DayData增加HR1的计数(其中_id ="2012_11_10").

  3. 如何为DayData(其中_id ="2012_11_10")(使用某些聚合函数)检索HR1,HR2,...,HR24的所有计数值的总和. p>

  4. 将所有小时的HourData计数转换为数组(对于任何DayData)的最佳方法是什么.例如对于具有_id ="2012_11_10"的DayData,我需要:

    int []计数= [100,200,300,...,2400]

编辑1

使用此代码,我打算获取HR1的HourData,其中_id = 1,使用_id ="2012_11_10"的DayData,但不返回任何内容.

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)

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) 在您的情况下,您只需检索DayData实例,然后显式求和所有需要的值:

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;

但这并不优雅.如果您想要一个优雅的解决方案,则需要将字段转换为数组,例如:

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

并与数组一起工作.让我知道是否有可能将其转换为数组.

4) 同样,对于您而言,没有任何优雅的解决方案.您可以做的只是遍历字段并创建一个数组:

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

或者您可以在课堂上直接创建枚举器,但我认为这对您来说太过分了.

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)

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.

这篇关于如何使用C#驱动程序在MongoDB中查询/更新子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆