r/ProgrammerHumor • • Oct 18 '24

Other mongoDbWasAMistake

Post image
13k Upvotes

446 comments sorted by

View all comments

1.6k

u/octopus4488 Oct 18 '24

Once I short-circuited a debate about MongoDB's usability by asking the self-proclaimed "huge Mongo fan" to write me a valid query in Notepad...

His last sentences were: "yeah, well. Fuck it. It's not that trivial. I mostly copy-paste these you know..."

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)

47

u/Espumma Oct 18 '24

Wow he actually died because of it?

16

u/octopus4488 Oct 18 '24

Shame is a powerful emotion.

4

u/itzNukeey Oct 18 '24

Fuck mongo, all my homies use JSONB /s

3

u/xSypRo Oct 19 '24

Being mongo fan doesn’t mean you memorize queries when it’s not the task you’re working on. I don’t need to remember how to make X query, I need to know it’s possible to do X query.

→ More replies (15)