Mongo DB通过subcollection属性进行查询收集

时间:2022-05-23 19:42:26

I have:

public class Movie : IMongoEntity
{
    public ObjectId Id { get; set; }
    public string Title { get; set; }
    public string Year { get; set; }
    public List<Actor> Actors { get; set; }
}

 public class Actor : IMongoEntity
{
    public ObjectId Id { get; set; }
    public string Name { get; set; }
}

If i want to retrieve the entire movie collection I do

如果我想要检索我的整个电影收藏

var query = this.MongoConnectionHandler.MongoCollection.FindAllAs<Movie>();

No I want to retrieve just the movies that have an actor with a certain name

不,我只想检索具有特定名称的演员的电影

I've tried something like:

我尝试过类似的东西:

IMongoQuery query = Query<Movie>.Where(m => m.Actors.Select(a => a.Name).Any(n => n.Contains(actorName)));

        var exc = this.MongoConnectionHandler.MongoCollection.Find(query);

But that won't work.

但那不行。

1 个解决方案

#1


0  

I think this should do the trick:

我认为这应该做的伎俩:

var query = Query<Movie>.ElemMatch(m => m.Actors, builder => builder.EQ(actor => actor.Name, actorName));
var exc = this.MongoConnectionHandler.MongoCollection.Find(query);

#1


0  

I think this should do the trick:

我认为这应该做的伎俩:

var query = Query<Movie>.ElemMatch(m => m.Actors, builder => builder.EQ(actor => actor.Name, actorName));
var exc = this.MongoConnectionHandler.MongoCollection.Find(query);