This would fit in a single SQL query which is expected to perform reasonably well, with an unstructured database optimizing this query will take months of work.
This would fit in a single SQL query which is expected to perform reasonably well, with an unstructured database optimizing this query will take months of work.
db.formEvent.aggregate([
{ $match: { createdAt: {$gte: ISODate("whenever")}}},
{ $group: { _id: "$custId", userAgents: { $addToSet: "$metadata.userAgent" } } },
{ $sort: { createdAt: -1 } }
])
Something like this? I'm not sure why this couldn't be an optimized query in mongo, but I'm also not sure why a query like this one needs to be optimized? This would run fast enough without needing indexes, and really fast with an index on a couple fields, but is a query like this run so frequently you need to have it be extremely optimized?If you can know for sure from the start that you'll never need queries like this, then of course something like Mongo will be awesome. But requirements change, hence this article.
At another company where I worked where we used Postgres, we had a multitenant set up where each of our (large) customers had their own database. The issue would have been the same.
You would no more “scan over all of your data” with Mongo with indexed fields than you would with an RDMS with indexes.
As requirements change, you do need to migrate your data into a schema that makes sense and that's regardless of whether you're using SQL or MongoDB.
In fact, if you are using C#. You could use the same LINQ syntax either way.