MongoDB嵌入式文档和C#/ POCO

时间:2015-01-15 21:45:43

标签: c# mongodb model-view-controller poco bson

我对C#/ MVC和MongoDB都比较陌生,我刚刚开始让POCO实现正常运行。但我仍然不确定如何将C#对象映射到更复杂的BSON文档。

我想看一些关于如何在具有嵌入式文档和数组等的BSON文档上进行基本CRUD操作的示例。另外,我非常注重效率,我想确保我我没有替换完整的BSON文档(可能大小为16MB),并且只对目标子文档(嵌入式文档)进行更改。

例如,这是一个MongoDB集合,我想在其上进行各种CRUD操作; (完全虚构的例子..)

    // customers collection
   { _id: new ObjectID(), 
    name: "Joe", 
    purchased: [
      {dte: ISODate("2015-01-01"), prod_id: ObjectId("xxx"), price: 256.95, desc: "Classic iPod"}, 
      {prod_id: ObjectId("yyy"), price: 19.95, desc: "Classic iPod earbuds"}
    ], 
    last_store: {address: "123 Abc St.", latlong: [123.234235, -9.0234234]}, 
    used_coupons: [
      {coupon_id: ObjectId("123"), 
       disc_type: "percent", 
       disc_value: 5, 
       applied_to: [
             { prod_id: ObjectId("xxx"), saved: 12.85 }, 
             { prod_id: ObjectId("yyy"), saved: 4.12 }
        ]}, 
    {coupon_id: ObjectId("098"), 
       disc_type: "usd", 
       disc_value: 20, 
       applied_to: [
             { prod_id: ObjectId("xxx"), saved: 20 }
        ]}
    ]
    }

任何人都可以向我展示如何映射到这种类型的复杂结构吗?另外,一些基本$push$addToSet$elemMatch示例也不错。

1 个答案:

答案 0 :(得分:3)

我环顾四周,发现了一个很棒的教程,通过制作POCO / MongoDB类库并有一些嵌入式文档示例。这是3期系列的最后一期。这是链接;

(Dr.Dobb's)带有C#的MongoDB:深度潜水

http://www.drdobbs.com/database/mongodb-with-c-deep-dive/240152181?pgno=1

这是一段摘录;

namespace RetrogamesWeb.Data.Services
{
    using System.Collections.Generic;

    using Entities;
    using MongoDB.Bson;
    using MongoDB.Driver;
    using MongoDB.Driver.Builders;

    public class PlayerService: EntityService<Player>
    {
        public void AddScore(string playerId, Score score)
        {
            var playerObjectId = new ObjectId(playerId);

            var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                    Query<Player>.EQ(p => p.Id, playerObjectId), 
                    Update<Player>.Push(p => p.Scores, score),
                    new MongoUpdateOptions
                    {
                        WriteConcern = WriteConcern.Acknowledged
                    });

            if (updateResult.DocumentsAffected == 0)
            {
                //// Something went wrong

            }
        }

        public IEnumerable<Player> GetPlayersDetails(int limit, int skip)
        {
            var playersCursor = this.MongoConnectionHandler.MongoCollection.FindAllAs<Player>()
                .SetSortOrder(SortBy<Player>.Ascending(p => p.Name))
                .SetLimit(limit)
                .SetSkip(skip)
                .SetFields(Fields<Player>.Include(p => p.Id, p => p.Name));
            return playersCursor;
        }

        public override void Update(Player entity)
        {
            var updateResult = this.MongoConnectionHandler.MongoCollection.Update(
                    Query<Player>.EQ(p => p.Id, entity.Id),
                    Update<Player>.Set(p => p.Name, entity.Name)
                        .Set(p => p.Gender, entity.Gender),
                    new MongoUpdateOptions
                    {
                        WriteConcern = WriteConcern.Acknowledged
                    });

            if (updateResult.DocumentsAffected == 0)
            {
                //// Something went wrong

            }
        }
    }
}