Skip to content

Instantly share code, notes, and snippets.

View c-eliasson's full-sized avatar

Christofer Eliasson c-eliasson

  • MarketHype
  • Marstrand, Sweden
View GitHub Profile
/*
* Example of a $some operator workaround using $expr. The document would match if
* purchasedProducts contain at least two of the three provided ObjectIds
*/
const purchasedProducts = [[ObjectId('ID1'), ObjectId('ID2'), ObjectId('ID3')]
db.collection.find({
purchasedProducts: { $in: purchasedProducts },
$expr: {
/*
* Workaround using aggregation pipeline stages to solve what a
* $some operator could have done.
*
* Inspired by this SO answer: https://stackoverflow.com/a/28161130/678801
*/
const purchasedProducts = [[ObjectId('ID1'), ObjectId('ID2'), ObjectId('ID3')];
db.collection.aggregate([
@c-eliasson
c-eliasson / some-operator-example.js
Last active October 30, 2019 21:07
Medium Story - The missing $some operator in MongoDB
/*
* Example of a $some operator where the document would match if purchasedDocuments
* contain at least two of the three provided ObjectIds
*/
db.collection.find({
purchasedProducts: {
$some: [[ObjectId('ID1'), ObjectId('ID2'), ObjectId('ID3')], { $gte: 2 }]
}
})
@c-eliasson
c-eliasson / generate-self-signed-cert.ps1
Created July 5, 2019 07:19
Reminder on how to generate a self-signed SSL certificate using PowerShell
$cert = New-SelfSignedCertificate -DnsName your.domain.com -CertStoreLocation "cert:\LocalMachine\My" -KeyLength 2048 -KeySpec "KeyExchange"
$password = ConvertTo-SecureString -String "Pa$$w0rd" -Force -AsPlainText
Export-PfxCertificate -Cert $cert -FilePath ".\your-certificate.pfx" -Password $password
@c-eliasson
c-eliasson / ReplayPoisonQueue.ts
Last active December 17, 2020 13:14
Azure Function for NodeJS that takes all messages in an Azure Storage poison queue and put them back on the original queue for a replay.
import AzureStorage from 'azure-storage'
import { Context, HttpRequest } from '@azure/functions'
import util from 'util'
const queueService = AzureStorage.createQueueService()
queueService.messageEncoder = new AzureStorage.QueueMessageEncoder.TextBase64QueueMessageEncoder()
const deleteMessage = util.promisify(queueService.deleteMessage).bind(queueService)
const createMessage = util.promisify(queueService.createMessage).bind(queueService)
const getMessage = util.promisify(queueService.getMessage).bind(queueService)
@c-eliasson
c-eliasson / chunk-array.ts
Created April 9, 2019 07:10
Split a TypeScript array of type T into chunks
/**
* Split an array into chunks
* @param array {T[]} Array to split
* @param size {number} Chunk size
*/
const chunkArray = <T>(array: T[], size: number): T[][] => {
const arr: T[][] = []
for (let i = 0, j = array.length; i < j; i += size) {
arr.push(array.slice(i, i + size));
}
@c-eliasson
c-eliasson / chunk-array
Created March 20, 2019 21:47
Split a JavaScript array into chunks
const chunkArray = (array, size) => {
const arr = []
for (let i = 0, j = array.length; i < j; i += size) {
arr.push(array.slice(i, i + size));
}
return arr
}
@c-eliasson
c-eliasson / array.prototypes.js
Last active December 13, 2015 19:58
A collection of convenient JavaScript-prototypes
// Internet Explorer did not support indexOf until IE9
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)