Skip to content

Instantly share code, notes, and snippets.

View kevcodez's full-sized avatar
🚀

K-Dog (Kevin) kevcodez

🚀
View GitHub Profile
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:48
medium_mongodb_performance_projection_remove_single_property
db.assets.find({}, {
projection: {
"lastPrice": 0
}
})
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:48
medium_mongodb_performance_projection_remove_multiple_properties
db.assets.find({}, {
projection: {
"lastPrice": 0,
"marketCap": 0
}
})
@kevcodez
kevcodez / query.sh
Last active October 7, 2022 15:08
medium_mongodb_performance_include_field
db.assets.find({
marketCap: { $gt: 10000 }
}, {
projection: { isin: 1 }
})
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:49
medium_mongodb_performance_projection_compute
db.assets.find({}, {
projection: {
marketCap: 1,
lastPrice: 1,
marketShares: { $divide: ["$marketCap", "$lastPrice"] }
}
})
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:49
medium_mongodb_performance_projection_slice
{
projection: {
myLargeArray: {
$slice: ['$myLargeArray', 0, 10]
}
}
}
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:49
medium_mongodb_performance_merge
db.holdings.aggregate([
{
$group: {
_id: '$portfolio',
assetIdentifiers: {
$addToSet: '$asset.identifier',
},
},
},
{
@kevcodez
kevcodez / query.sh
Created October 7, 2022 14:50
medium_mongodb_performance_read_pref
db.assets
.find({ ipoDate: { $gt: ISODate('2022-01-01') } })
.readPref('secondaryPreferred')
@kevcodez
kevcodez / mongo.ts
Created October 7, 2022 15:02
medium_mongodb_performance_node_snappy
const client = await MongoClient.connect(mongoConfig().mongoUrl, {
compressors: 'snappy', // make sure Snappy is installed
});
@kevcodez
kevcodez / parallel.ts
Created October 7, 2022 15:02
medium_mongodb_performance_parallel_fetching
async function getQuotesForPeriod(
assetIdentifier: string,
from: Date,
to: Date
): Promise<Quote[]> {
const quotesCollection = await getQuotesCollection();
/**
* When fetching multiple years of data, queries are fast, but retrieval not so much.
* To improve query performance, we split the interval into a few chunks and query/fetch the data in parallel,
@kevcodez
kevcodez / pooling.ts
Last active April 20, 2025 03:01
medium_mongodb_performance_pooling
const client = await MongoClient.connect('mongodb-url', {
minPoolSize: 20,
maxPoolSize: 100,
});