Skip to content

Instantly share code, notes, and snippets.

View shadyrudy's full-sized avatar
🍃
Doing the MongoDB thing

Rudy Rodarte shadyrudy

🍃
Doing the MongoDB thing
View GitHub Profile
@shadyrudy
shadyrudy / DateLoop.sql
Created August 14, 2021 21:02
Loop over a date range in SQL Server
declare @StartDate date
declare @EndDate date
declare @OverallEndDate date
declare @PrintStart varchar(10)
declare @PrintEnd varchar(10)
-- Set the start date and overall end date
-- Set the end date to 7 days after the start date
set @OverallEndDate = '2018-12-31'
set @StartDate = '2018-01-01'
@shadyrudy
shadyrudy / DateFormats.sql
Created October 1, 2021 15:16
Useful date formats in SQL Server
/**
* The following are a few useful date formats in SQL Server
* These can be used for various types of output such as status
* or naming files.
*/
select convert(varchar(10), getdate(), 120); /* YYYY-MM-dd */
select convert(varchar(16), getdate(), 120); /* YYYY-MM-dd HH:mm */
select convert(varchar(20), getdate(), 112); /* YYYYMMdd */
select format(getdate(), 'yyyyMMdd_HHmm', 'en-US'); /* YYYYMMdd_HHmm */
@shadyrudy
shadyrudy / init-rs.js.j2
Created July 7, 2022 21:16
A j2 file for initilzing a MongoDB replica set. Needs an ansible inventory lists of hosts to work.
rs.initiate(
{
_id: "{{ group_names[0] }}",
members: [
{% for h in groups[group_names[0] ]%}
{ _id: {{ loop.index }}, host : "{{ h }}":
{%if hostvars[inventory_hostname].group_names[0].startswith('shard') %}
{{ shard_port }}
{% else %}
{{ cfgserver_port }}
@shadyrudy
shadyrudy / mongod.conf
Created December 6, 2022 19:46
Default mongod.conf for MongoDB 6.0 - Percona distribution
# mongod.conf, Percona Server for MongoDB
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine: wiredTiger
@shadyrudy
shadyrudy / listAllMongoDBUsers.js
Created January 19, 2023 20:01
List all users and roles in MongoDB
// The following MongoDB script displays all users and roles for all databases.
// Appropriate permissions are required to run this script
use admin
// Increase the number of documents returned by the find command
// Use either the legacy mongo shell version or Mongosh version
// Legacy Mongo Shell version
DBQuery.shellBatchSize = 300
// Mongosh version
@shadyrudy
shadyrudy / Export-SqlReplicationArticles .ps1
Created February 22, 2023 04:55
Exports SQL Server replication articles
function Export-SqlReplicationArticles {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$ServerInstance,
[Parameter(Mandatory=$true)]
[string]$DatabaseName,
[Parameter(Mandatory=$true)]
@shadyrudy
shadyrudy / Get-DbaReplicationArticle.ps1
Created February 22, 2023 05:07
Get replication articles from the distribution database.
function Get-DbaReplicationArticle {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$SqlInstance,
[Parameter(Mandatory=$true)]
[string]$Database
)
@shadyrudy
shadyrudy / updateStats.sql
Created March 2, 2023 16:00
Update SQL Server Statistics
-- The following command will generate the
-- Statistics Rebuild command for all of the
-- statistics on a the targeted schema and table
select 'update statistics ' + sc.name + '.' + so.name + '(' + stat.name + ') with fullscan;' as Script
, stats_date(stat.object_id, stat.stats_id) as last_updated
, stuff((
select ', ' + cols.name
from sys.stats_columns as statcols
join sys.columns as cols
on statcols.column_id = cols.column_id
@shadyrudy
shadyrudy / AGListener.ps1
Created June 14, 2023 21:20
Get SQL Server AG Listener Name via Powershell
# Import SQL Server module
Import-Module "SQLPS" -DisableNameChecking
# Variables
$ServerName = "YourServerName" # Enter your server name here
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server $ServerName
# Get the availability groups
$AvailabilityGroups = $Server.AvailabilityGroups
@shadyrudy
shadyrudy / postgres-permissions.sql
Last active November 9, 2023 22:52
PostgreSQL grant information. View postgres permission information
-- A Quick query to view postgresql grant information
-- Work in progress
SELECT
'TABLE' AS object_type,
grantee,
table_schema,
table_name,
privilege_type
FROM
information_schema.role_table_grants