简体   繁体   中英

How to filter Bson Document on a computed field of a C# class?

I have several classes that I need to store in a MongoDb collection, these classes have a common base class and common interface defining the common fields. Every class have a computed field with the same name but with different implementations. When I try to query on that field I get the message that the field is not supported

I'm running this on a .Net Core 2.2 console application with the latest MongoDb Driver

Interface:

 public interface ITask
    {
        ObjectId Id { get; set; }
        TaskStatus Status { get; set; }
        DateTime Timestamp { get; set; }
        string UserId { get; set; }
        string ComputedField{ get; }
    }

Base Class:

public abstract class BaseTask : ITask
    {
        [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
        public ObjectId Id { get; set; }
        [BsonElement("Status")]
        public TaskStatus Status { get; set; }
        [BsonElement("Timestamp")]
        public DateTime Timestamp { get; set; }
        [BsonElement("UserId")]
        public string UserId { get; set; }
        public virtual string ComputedField
        {
            get { return CalculateMD5Hash(Id.ToString()); }
        }
}

Actual class that give me the problem

public class MyTask : BaseTask
    {

        [BsonElement("Field1")]
        public Guid Field1{ get; set; }


        [BsonElement("ComputedField")]
        public override string ComputedField
        {
            get { return CalculateMD5Hash($"ABC{Field1.ToString()}"); } 
        }

    }

Wrapper that call the MongoDb layer

public class TaskService<T> : ITaskService<T> where T : ITask
    {
        private readonly IPersistanceLayer<ObjectId, object> _pl;


        public async Task<T> GetNextTask(string key)
        {
              var oee = _pl.Mongo.Filter<T>(x => x.ConcurrencyKey==key 
                , typeof(T).Name).OrderBy(x=> x.Timestamp).FirstOrDefault();
            return oee;
        }

    }

MongoDb Layer

 public IEnumerable<T> Filter<T>(System.Linq.Expressions.Expression<Func<T, bool>> lambda, string collection)
        {
            var filter = Builders<T>.Filter.Where(lambda);

            return _db.GetCollection<T>(collection).Find(filter).ToList();
        }

The error I get is

[ERR] {document}.ComputedField is not supported.

In the collection i see the document saved when I insert it but and the value of the computed filed is stored correctly, the only problem is when i try to query on that field

The problem was that I was looking in the wrong collection and casting to the wrong class

public class TaskService<T> : ITaskService<T> where T : ITask
    {
        private readonly IPersistanceLayer<ObjectId, object> _pl;


        public async Task<T> GetNextTask(string key)
        {
              var nextTask= _pl.Mongo.Filter<T>(x => x.ConcurrencyKey==key 
                , typeof(T).Name).OrderBy(x=> x.Timestamp).FirstOrDefault();
            return nextTask;
        }

    }

In this method T was of the type BaseTask and not MyTask so my computed field was not there, I have corrected that and now it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM