2
public class MongoDbRepository<T> : IRepository<T> where T : IEntityBase
{
    private IMongoDatabase database;
    private IMongoCollection<T> collection;

    public MongoDbRepository()
    {
        GetDatabase();
        GetCollection();
    }

    private void GetDatabase()
    {
        var client = new MongoClient(GetConnectionString());
        database = client.GetDatabase(GetDatabaseName());
    }

    private void GetCollection()
    {
        collection = database.GetCollection<T>(typeof(T).Name);
    }

    private string GetDatabaseName()
    {
        return ConfigurationManager.AppSettings.Get("MongoDbDatabaseName");
    }

    private string GetConnectionString()
    {
        return ConfigurationManager.AppSettings.Get("MongoDbConnectionString").Replace("{DB_NAME}", GetDatabaseName());
    }


    public async Task<IList<T>> SelectAllSync() 
    {
     //Add _id that is unique for each document
     var filter = new BsonDocument("_id", new BsonDocument("$exists", true));
     var people = await collection.Find<T>(filter).ToListAsync<T>();

        //Search methods
        var result1 = await database.GetCollection<T>(typeof(T).Name)
                                .Aggregate()
                                .Match(x => x.last_name.Equals("Hall")).ToListAsync();


        var result2 = collection.FindAsync<T>(Builders<T>
                            .Filter.Eq(x => x.last_name, "Grammer")
                            , new FindOptions<T> { Comment = "TEST" }
                            , CancellationToken.None);

        var result3 = await collection.FindAsync<T>(
                           Builders<T>.Filter.Eq("dealership_name", "D-PATRICK NISSAN"),
                           new FindOptions<T> { Comment = "TEST" },
                           CancellationToken.None);



        return (IList<T>)people;
    }

    public Task Insert(T entity)
    {
        var result = collection.InsertOneAsync(entity);
        return  result;
    }

    public async Task<DeleteResult> Delete(T entity)
    {
        var deleteResult = await collection.DeleteOneAsync(Builders<T>.Filter.Eq(s => s._id, entity._id));
        return deleteResult;
    }

    public async Task<IList<T>> SearchFor(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        var result = await database.GetCollection<T>(typeof(T).Name)
                                .Aggregate()
                                .Match(predicate).ToListAsync();
        return result;
    }

    public async Task<IList<T>> GetById(ObjectId id)
    {
        var result = await database.GetCollection<T>(typeof(T).Name)
                                .Aggregate()
                                .Match(x => x._id.Equals(id)).ToListAsync();
        return result;
    }

    public async Task<UpdateResult> Update(T entity)
    {
        if (entity._id == null)
            await Insert(entity);

// deal_code の文字列値のリストを追加

        var list = new List<string>();
        list.Add("dealer_code");
        var result = await collection.UpdateOneAsync(
             Builders<T>.Filter.Eq(s => s.dealer_code, entity.dealer_code),
             Builders<T>.Update.AddToSet(list[0], entity.dealer_code)
                                      );

        return result;
    }
}

Update メソッドに問題があります。それは私に与えました:

「フィールドのシリアライザーは、アイテムのシリアル化情報を'dealer_code'実装して提供する必要があります。」IBsonArraySerializer

どういう意味ですか ?どうすれば修正できますか?

4

2 に答える 2

2
 public class MongoDbRepository<T> : IRepository<T> where T : IEntityBase
{
    private IMongoDatabase database;
    private IMongoCollection<T> collection;

    public MongoDbRepository()
    {
        GetDatabase();
        GetCollection();
    }

    private void GetDatabase()
    {
        var client = new MongoClient(GetConnectionString());
        database = client.GetDatabase(GetDatabaseName());
    }

    private void GetCollection()
    {
        collection = database.GetCollection<T>(typeof(T).Name);
    }

    private string GetDatabaseName()
    {
        return ConfigurationManager.AppSettings.Get("MongoDbDatabaseName");
    }

    private string GetConnectionString()
    {
        return ConfigurationManager.AppSettings.Get("MongoDbConnectionString").Replace("{DB_NAME}", GetDatabaseName());
    }


    public async Task<IList<T>> SelectAllSync() 
    {

        //var filter = new BsonDocument("dealer_code", new BsonDocument("$exists", true));
        //var people = await collection.Find<T>(filter).ToListAsync<T>();

        //Search methods
        var result1 = await database.GetCollection<T>(typeof(T).Name)
                                .Aggregate()
                                .Match(x => x.last_name.Equals("Hall")).ToListAsync();


        var result2 = collection.FindAsync<T>(Builders<T>
                            .Filter.Eq(x => x.last_name, "Grammer")
                            , new FindOptions<T> { Comment = "TEST" }
                            , CancellationToken.None);





        return (IList<T>)result1;
    }

    public Task Insert(T entity)
    {
        var result = collection.InsertOneAsync(entity);
        return  result;
    }

    public async Task<DeleteResult> Delete(T entity)
    {
        var deleteResult = await collection.DeleteOneAsync(Builders<T>.Filter.Eq(s => s._id, entity._id));
        return deleteResult;
    }

    public async Task<IList<T>> SearchFor(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        var result = await database.GetCollection<T>(typeof(T).Name)
                                .Aggregate()
                                .Match(predicate).ToListAsync();
        return result;
    }

    public async Task<IList<T>> GetById(ObjectId id)
    {
        var result = await database.GetCollection<T>(typeof(T).Name)
                                .Aggregate()
                                .Match(x => x._id.Equals(id)).ToListAsync();
        return result;
    }

    public async Task<UpdateResult> Update(Expression<Func<T, bool>> filter,T entity) 
    {
        if (entity._id == null)
            await Insert(entity);

        var list = new List<string>();
        list.Add("dealer_code");

        var result = await collection.UpdateOneAsync(
                                Builders<T>.Filter.Where(filter),
                                Builders<T>.Update.Set(x=>x.dealer_code, entity.dealer_code));

        if (result.IsAcknowledged)
        {
            Console.WriteLine("Success");
        }
        return result;
    }
}

最後に、 Update.AddToSet() の代わりに Update.Set() を変更すると機能し始めます

于 2015-05-13T09:13:18.650 に答える
0

$addToSet私の仮定は、セットではないものを呼び出そうとしているということです:

// ... 
Builders<T>.Update.AddToSet("dealer_code", entity.dealer_code)
// ...

しかし、コメントで説明したように、dealer_codeタイプはstringです。要素がセットである場合にのみ、何かをセットに追加できます (たとえば、 astring[]または a ) List<string>。達成したいことは完全に明確ではありませんが、に変更string dealer_codeしてみてくださいList<string> dealer_codes

于 2015-05-12T19:49:50.183 に答える