private bool UserExists(IChannelSourceHandle channelSource, ICoreSessionHandle session, CancellationToken cancellationToken)
        {
            try
            {
                var command   = new BsonDocument("usersInfo", _username);
                var operation = new ReadCommandOperation <BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
                var result    = operation.Execute(channelSource, ReadPreference.Primary, session, cancellationToken);

                BsonValue users;
                if (result.TryGetValue("users", out users) && users.IsBsonArray && users.AsBsonArray.Count > 0)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (MongoCommandException ex)
            {
                if (ex.Code == 13)
                {
                    return(false);
                }

                throw;
            }
        }
Example #2
0
        // methods
        public IEnumerable <BsonDocument> Execute(IReadBinding binding, CancellationToken cancellationToken)
        {
            var filter    = _username == null ? (BsonValue)1 : _username;
            var command   = new BsonDocument("usersInfo", filter);
            var operation = new ReadCommandOperation <BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var result    = operation.Execute(binding, cancellationToken);

            BsonValue users;

            if (result.TryGetValue("users", out users) && users.IsBsonArray)
            {
                return(users.AsBsonArray.Select(v => v.AsBsonDocument));
            }
            else
            {
                return(Enumerable.Empty <BsonDocument>());
            }
        }
 public static string GetStorageEngine()
 {
     using (var binding = GetReadWriteBinding())
     {
         var       command   = new BsonDocument("serverStatus", 1);
         var       operation = new ReadCommandOperation <BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
         var       response  = operation.Execute(binding, CancellationToken.None);
         BsonValue storageEngine;
         if (response.TryGetValue("storageEngine", out storageEngine) && storageEngine.AsBsonDocument.Contains("name"))
         {
             return(storageEngine["name"].AsString);
         }
         else
         {
             return("mmapv1");
         }
     }
 }
 public static IEnumerable <string> GetModules()
 {
     using (var binding = GetReadBinding())
     {
         var       command   = new BsonDocument("buildinfo", 1);
         var       operation = new ReadCommandOperation <BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
         var       response  = operation.Execute(binding, CancellationToken.None);
         BsonValue modules;
         if (response.TryGetValue("modules", out modules))
         {
             return(modules.AsBsonArray.Select(x => x.ToString()));
         }
         else
         {
             return(Enumerable.Empty <string>());
         }
     }
 }
        private TCommandResult RunReadCommandAs <TCommandResult>(
            BsonDocument command,
            IBsonSerializer <TCommandResult> resultSerializer,
            ReadPreference readPreference,
            out MongoServerInstance serverInstance)
            where TCommandResult : CommandResult
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new ReadCommandOperation <TCommandResult>(new DatabaseNamespace(_name), command, resultSerializer, messageEncoderSettings);

            using (var binding = _server.GetReadBinding(readPreference))
                using (var connectionSource = binding.GetReadConnectionSource())
                {
                    var endPoint = (DnsEndPoint)connectionSource.ServerDescription.EndPoint;
                    var address  = new MongoServerAddress(endPoint.Host, endPoint.Port);
                    serverInstance = _server.GetServerInstance(address);
                    return(operation.Execute(connectionSource, readPreference, Timeout.InfiniteTimeSpan, CancellationToken.None));
                }
        }
 public static string GetStorageEngine()
 {
     using (var binding = GetReadWriteBinding())
     {
         var command = new BsonDocument("serverStatus", 1);
         var operation = new ReadCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
         var response = operation.Execute(binding, CancellationToken.None);
         BsonValue storageEngine;
         if (response.TryGetValue("storageEngine", out storageEngine) && storageEngine.AsBsonDocument.Contains("name"))
         {
             return storageEngine["name"].AsString;
         }
         else
         {
             return "mmapv1";
         }
     }
 }
 public static IEnumerable<string> GetModules()
 {
     using (var binding = GetReadBinding())
     {
         var command = new BsonDocument("buildinfo", 1);
         var operation = new ReadCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, __messageEncoderSettings);
         var response = operation.Execute(binding, CancellationToken.None);
         BsonValue modules;
         if (response.TryGetValue("modules", out modules))
         {
             return modules.AsBsonArray.Select(x => x.ToString());
         }
         else
         {
             return Enumerable.Empty<string>();
         }
     }
 }
        private BsonDocument GetCollectionStats(IReadBinding binding, bool async)
        {
            var command = new BsonDocument
            {
                { "collStats", _collectionNamespace.CollectionName }
            };
            var operation = new ReadCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            if (async)
            {
                return operation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                return operation.Execute(binding, CancellationToken.None);
            }
        }
 private BsonDocument GetCollectionInfo(IReadBinding binding, string collectionName)
 {
     var commandOperation = new ReadCommandOperation<BsonDocument>(
         _collectionNamespace.DatabaseNamespace,
         new BsonDocument("listCollections", 1),
         BsonDocumentSerializer.Instance,
         new MessageEncoderSettings());
     var commandResult = commandOperation.Execute(binding, CancellationToken.None);
     return commandResult["cursor"]["firstBatch"].AsBsonArray.Where(c => c["name"] == _collectionNamespace.CollectionName).Single().AsBsonDocument;
 }