Пример #1
0
        /// <summary>
        /// Resolves a collection of document references.
        /// Collection name of the referenced documents must be specified, database name is ignored.
        /// </summary>
        public static IReadOnlyCollection <T> LoadAll <T>(this DocRefList <T> docRef, IMongoDatabase database)
        {
            if (string.IsNullOrEmpty(docRef.Collection))
            {
                throw new InvalidOperationException($"The DocRef does not specify the collection of the referenced document. Try Load(IMongoCollection).");
            }

            return(docRef.LoadAll(database.GetCollection <T>(docRef.Collection)));
        }
Пример #2
0
        /// <summary>
        /// Resolves a collection of document references. Collection name and database name are ignored.
        /// </summary>
        public static IReadOnlyCollection <T> LoadAll <T>(this DocRefList <T> docRef, IMongoCollection <T> collection)
        {
            // Less efficient approach, preserves ordering
            return(docRef.Ids
                   .Select(id => collection.Find(Builders <T> .Filter.Eq("_id", id)).First())
                   .ToList());

            // Simple, efficient approach (unfortunately doesn't preserve ordering):
            // return collection.Find(Builders<T>.Filter.In("_id", docRef.Ids)).ToList();
        }