我有以下型号:
基类:
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; }
}
}
这个名字是独一无二的 渠道类
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; }
}
}
每日节目。 name属性是存储为ddMMyyyy的日期:
public class DailyPrograming : Identifiable{
public DailyPrograming(){
DailyPrograms = new List<Program>(30);
}
public IList<Program> DailyPrograms { get; set; }
}
节目:
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; }
}
}
现在,我想使用以下方法过滤特定日期的某个频道的节目:
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;
}
但它永远不会返回现有数据。如何检索日期频道的节目? -
答案 0 :(得分:10)
var builder = Builders<BsonDocument>.Filter;
var filt = builder.Eq("Price", "9.20") & builder.Eq("ProductName","WH-208");
var list =await collection.Find(filt).ToListAsync();
我们可以使用'&amp;'而不是使用$和。 Reference link
答案 1 :(得分:2)
根据您的示例,我使用了id = "54c00c65c215161c7ce2a77c"
和prgDate = "2212015"
然后我将查询更改为:
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();
此查询正常
有一点:
您的收藏类型为
Chanel
而非DailyPrograming
如果您的收藏品是Chanel,则必须使用Query<Channel>
并通过DailyPrograming
查询嵌套Query<Channel>.EQ(c => c.DailyPrograming.Name, "2212015")