MongoDB C # driver, respectively. Linq: querying for unique instances and using AsQueryable static properties

I am working on a rather large MongoDB database and wondering if there is a more efficient way for some queries. For example, I store many instances Gamein db, where the class Gamelooks like this:

public class Game{
    [BsonId]
    public long ID { get; set; }

    // ... some more properties

    public List<Player> Players { get; set; }
}

where each instance of the game has a list Player, which, in turn, has some properties, such as Name.

In one presentation model, I want to bind ComboBoxall the players in the database to the name, but remember that the same player could play many games. I load them as follows:

private void LoadPlayersNames() {
    var _l = StaticMongo.GetGames.SelectMany(n => n.Players).Select(n => n.Name);
    PlayerNames = new ObservableCollection<string>(new HashSet<string>(_l));
}

StaticMongo MongoClient IMongoDatabase, GetGames StaticMongo :

private readonly static IMongoDatabase _database;
public static IMongoQueryable<DetailedHand> GetGames { 
    get {
        return _database.GetCollection<Game>("Games").AsQueryable();
    } 
}

, , :

1: ( HashSet), . 2: MongoDB AsQueryable ?

, , , .

+4
1

, , :

mongo :

db.games.aggregate( 
     { 
        "$unwind": "$players"   
     },   
     { 
        "$group": { 
            "_id": "$players.name",             
        }  
    }
 )

MongoDB # db #.

Update:

# . . , AsQueryable()

var collection = _database.GetCollection<BsonDocument>("games");
var pipeline = collection.Aggregate()
    .Unwind(i => i.players)
    .Group(new BsonDocument { { "_id", "$players.name" } });

, .

0

Source: https://habr.com/ru/post/1613089/


All Articles