Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering mongodb data

I have the following model:

Base class:

public abstract class Identifiable{
    private ObjectId id;
    private string name;

    protected Identifiable(){
        id = ObjectId.GenerateNewId();
    }

    [BsonId]
    public ObjectId Id{
        get { return id; }
        set { id = value; }
    }

    [BsonRequired]
    public string Name{
        get { return name; }
        set { name = value; }
    }
}

The name is unique.
A channel class

 public class Channel : Identifiable{
    private DateTime creationDate;

    private string url;
    private DailyPrograming dailyPrograming;

    public DailyPrograming DailyPrograming{
        get { return dailyPrograming; }
        set { dailyPrograming = value; }
    }

    public DateTime CreationDate{
        get { return creationDate; }
        set { creationDate = value; }
    }

    public string Url{
        get { return url; }
        set { url = value; }
    }
}

Daily programs. The name property is the date stored as ddMMyyyy:

public class DailyPrograming : Identifiable{
    public DailyPrograming(){
        DailyPrograms = new List<Program>(30);
    }

    public IList<Program> DailyPrograms { get; set; }
}

The programs:

public class Program : Identifiable{
    private DateTime programDate;
    private string category;
    private string description;

    public DateTime ProgramDate{
        get { return programDate; }
        set { programDate = value; }
    }

    public string Category{
        get { return category; }
        set { category = value; }
    }

    public string Description{
        get { return description; }
        set { description = value; }
    }
}

Now, I want to filter the program of certain channel for specific date using:

public DailyPrograming GetProgramsForDate(string channelId, string prgDate){
        ObjectId id = new ObjectId(channelId);
        IMongoQuery query = Query.And(Query<Channel>.EQ(c => c.Id, id), 
            Query<DailyPrograming>.EQ(dp => dp.Name, prgDate));
        var result = Database.GetCollection<DailyPrograming>(CollectionName).Find(query).FirstOrDefault();
        return result;
    }

But it never returns the existing data. How to retrieve the programings of a channel for a date? -

like image 828
Oscar Avatar asked Jan 22 '15 17:01

Oscar


2 Answers

var builder = Builders<BsonDocument>.Filter;
var filt = builder.Eq("Price", "9.20")
         & builder.Eq("ProductName", "WH-208");
var list = await collection.Find(filt).ToListAsync();

We can use & instead of $and. See this post, for another example.

like image 123
Priya Avatar answered Sep 21 '22 15:09

Priya


According to your sample I used id = "54c00c65c215161c7ce2a77c" and prgDate = "2212015"

then I changed the query to this:

var collection = database.GetCollection<Channel>("test6");
var id = new ObjectId("54c00c65c215161c7ce2a77c");
var query = Query.And(Query<Channel>.EQ(c => c.Id, id), Query<Channel>.EQ(c => c.DailyPrograming.Name, "2212015"));
var result = collection.Find(query).FirstOrDefault();

this query works fine

Some point:

Your collection type is Chanel not DailyPrograming When your collection is Chanel you have to use Query<Channel> and query nested DailyPrograming via Query<Channel>.EQ(c => c.DailyPrograming.Name, "2212015")

like image 28
2 revs, 2 users 67% Avatar answered Sep 20 '22 15:09

2 revs, 2 users 67%