QueryValue() public static method

public static QueryValue ( BsonDocument doc, IEnumerable keys ) : BsonValue
doc BsonDocument
keys IEnumerable
return BsonValue
        public async Task <TrackableDictionary <TKey, TValue> > LoadAsync(IMongoCollection <BsonDocument> collection,
                                                                          params object[] keyValues)
        {
            if (keyValues.Length == 0)
            {
                throw new ArgumentException("At least 1 keyValue required.");
            }

            BsonDocument doc;

            if (keyValues.Length == 1)
            {
                doc = await collection.Find(Builders <BsonDocument> .Filter.Eq("_id", keyValues[0])).FirstOrDefaultAsync();
            }
            else
            {
                // partial query

                var partialKeys = keyValues.Skip(1);
                var partialPath = DocumentHelper.ToDotPath(partialKeys);
                var partialDoc  = await collection.Find(Builders <BsonDocument> .Filter.Eq("_id", keyValues[0]))
                                  .Project(Builders <BsonDocument> .Projection.Include(partialPath))
                                  .FirstOrDefaultAsync();

                doc = DocumentHelper.QueryValue(partialDoc, partialKeys) as BsonDocument;
            }

            if (doc == null)
            {
                return(null);
            }

            return(ConvertToTrackableDictionary(doc));
        }
Example #2
0
        public async Task <TrackableList <T> > LoadAsync(IMongoCollection <BsonDocument> collection,
                                                         params object[] keyValues)
        {
            if (keyValues.Length < 2)
            {
                throw new ArgumentException("At least 2 keyValue required.");
            }

            // partial query

            var keyPath    = keyValues.Length > 1 ? DocumentHelper.ToDotPath(keyValues.Skip(1)) : "";
            var partialDoc = await collection.Find(Builders <BsonDocument> .Filter.Eq("_id", keyValues[0]))
                             .Project(Builders <BsonDocument> .Projection.Include(keyPath))
                             .FirstOrDefaultAsync();

            if (partialDoc == null)
            {
                return(null);
            }

            var doc = DocumentHelper.QueryValue(partialDoc, keyValues.Skip(1));

            if (doc == null)
            {
                return(null);
            }

            if (doc.IsBsonArray == false)
            {
                throw new Exception($"Data should be an array. ({doc.BsonType})");
            }

            return(ConvertToTrackableList(doc.AsBsonArray));
        }
        public T ConvertToTrackablePoco(BsonDocument doc, params object[] partialKeys)
        {
            var partialDoc = DocumentHelper.QueryValue(doc, partialKeys);

            if (partialDoc == null)
            {
                return(default(T));
            }

            return(ConvertToTrackablePoco(partialDoc.AsBsonDocument));
        }
Example #4
0
        public TrackableList <T> ConvertToTrackableList(BsonDocument doc, params object[] partialKeys)
        {
            var partialDoc = DocumentHelper.QueryValue(doc, partialKeys);

            if (partialDoc == null)
            {
                return(null);
            }

            var list = new TrackableList <T>();

            ConvertToList(partialDoc.AsBsonArray, list);
            return(list);
        }
        public TrackableDictionary <TKey, TValue> ConvertToTrackableDictionary(BsonDocument doc,
                                                                               params object[] partialKeys)
        {
            var partialDoc = DocumentHelper.QueryValue(doc, partialKeys);

            if (partialDoc == null)
            {
                return(null);
            }

            var dictionary = new TrackableDictionary <TKey, TValue>();

            ConvertToDictionary(partialDoc.AsBsonDocument, dictionary);
            return(dictionary);
        }