r/ProgrammerHumor • • Oct 18 '24

Other mongoDbWasAMistake

Post image
13k Upvotes

446 comments sorted by

View all comments

Show parent comment

291

u/rastaman1994 Oct 18 '24

I'm indifferent in this debate, but everyone I work with can do this for regular find/update/delete operations.

What were you asking anyway? Aggregation pipelines do become complex.

176

u/octopus4488 Oct 18 '24

A simple find with a where clause.

And test them with a notepad. :)

183

u/Z21VR Oct 18 '24

I hate mongoDB, fiercly i'd say, but the fact they cant write a simple query with 1 where clause is more on them than on mongoDB. Still, fuck mongoDB

115

u/rastaman1994 Oct 18 '24

db.redditors.find({ 'skeptical': true });

Sent from my Android

34

u/[deleted] Oct 18 '24 edited Oct 19 '24
db.redditors.find({"skeptical": true});

Need to use double quotes, ", not “ or ” or ‘ or ’ or '

Need to quote booleans.

Though looks like unquoted booleans is part of the spec, so idk if it’s supported.

Double quotes still the standard, double checked.

https://www.json.org/json-en.html

Edit: saying it’s valid JavaScript and not valid json just makes it even weirder.

That means mongodb forces you to parse the json, to send to it as a JavaScript object, which it then dumps to bson, to send., instead of just having the query in a file you can read and send without intermediate parsing.

37

u/rastaman1994 Oct 18 '24

Single and double quotes work, true as a string I've never tried but won't work I imagine

3

u/[deleted] Oct 18 '24

Even the single ‘smart quote’ that isn't the normal single quote ?

4

u/rastaman1994 Oct 18 '24

Might be a formatting thing in your client, I typed regular single quotes. The true without quotes I'm 100% sure about, I use it almost daily.

3

u/[deleted] Oct 18 '24

Ya, confirmed the unquoted Boolean is in-spec. That’s my mistake.

Smart quotes looks like on my end as well. So all I got is double quotes :D

17

u/rastaman1994 Oct 18 '24

Fyi, Json is not the same as mongodb json.

→ More replies (1)

14

u/theturtlemafiamusic Oct 18 '24

Single quotes will work fine in pretty much any MongoDB client. Also you don't need the quotes around "skeptical" at all.

And JSON and MongoDB JSON are not exactly the same.

This is valid MongoDB JSON for example

{ name: { $regex: /acme.*corp/i, $nin: [ 'acmeblahcorp' ] } }

https://www.mongodb.com/docs/manual/reference/operator/query/regex/

6

u/Engine_Light_On Oct 18 '24

Json boolean does not have quotes, else it becomes strings.

→ More replies (1)

2

u/hyrumwhite Oct 18 '24

If you’re in js land, single or double quotes is down to the whims of whoever set up your formatter.  This is a valid object in JS: 

{ 

skeptical: true,

"skeptical": true,

'skeptical':true,

`skeptical`:true,

[someVarWithValueSkeptical]: true

 }

→ More replies (7)

2

u/louis-lau Oct 19 '24

Mongo's shell is javascript based, not JSON based. Their query is perfectly valid.

2

u/Stummi Oct 19 '24

You don't even need to quote the field name afaik

1

u/[deleted] Oct 19 '24

If you stringify it, you get the double quotes.

→ More replies (2)

2

u/QuittingToLive Oct 18 '24

> all rows returned

13

u/daern2 Oct 18 '24

Well your "huge mongo fan" was not a "huge mongo expert" then, or they'd have had no issues churning out queries and aggregate pipelines into a text editor.

(Source: me, someone who has worked with mongodb and, even better, knows how to use it too)

8

u/[deleted] Oct 18 '24

[removed] — view removed comment

0

u/Rogork Oct 18 '24

For one to one relations? Sure, for one to many and many to many? Absolute nightmare returned data that you have to deal with.

Not that MongoDB's aggregation pipeline with multiple (or nested) lookups is intuitive or easy to use, but it is powerful enough if you know how to use it and returns fully usable dataset without further processing on the application-side.

3

u/[deleted] Oct 18 '24

[removed] — view removed comment

3

u/Rogork Oct 19 '24

That depends on your requirements doesn't it? For instance placing the same product under multiple categories, the select query for this in SQL would fetch you a separate row for all the categories the product is in, and you'd process this in your application, whereas in MongoDB for instance you'd get the result instantly.

1

u/[deleted] Oct 19 '24

[removed] — view removed comment

1

u/Rogork Oct 19 '24

Creating a separate table for the many-to-many relation is the table's normalization, when querying it would look something like this (assuming you want all the category data):

SELECT * FROM products p LEFT JOIN products_categories pc ON p.Product_ID = pc.ProductID LEFT JOIN categories c ON c.Category_ID = pc.Category_ID

Product_IDProductCategory_IDCategory
1TV1Electronics
1TV2Home Appliances
2Fridge2Home Appliances

This is where you have to aggregate and process the different rows application side, whereas a MongoDB query for the same concept would require only 2 tables (products table with the category IDs array field, and categories table):

db.getCollection("products").aggregate([
    {
        "$lookup": {
            "from": "categories",
            "localField": "Category_IDs",
            "foreignField": "Category_ID",
            "as": "categories"
        }
    }
]);

Which returns (what you would realistically want anyway):

[
    {
        "_id": 1,
        "name": "TV",
        "categories": [
            { "_id": 1, "name": "Electronics" },
            { "_id": 2, "name": "Home Appliances" }
        ]
    },
    {
        "_id": 2,
        "name": "TV",
        "categories": [
            { "_id": 1, "name": "Electronics" }
        ]
    }
]
→ More replies (3)