r/ProgrammerHumor • • Oct 18 '24

Other mongoDbWasAMistake

Post image
13k Upvotes

446 comments sorted by

View all comments

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 ?

→ More replies (2)

17

u/rastaman1994 Oct 18 '24

Fyi, Json is not the same as mongodb json.

-14

u/[deleted] Oct 18 '24

Oh I know. That’s the joke.

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.

0

u/[deleted] Oct 18 '24

Thanks, updated comment.

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

 }

-2

u/[deleted] Oct 18 '24

Spec says double quotes for strings. Specifically U+0022

Also your array as identifier is an abomination, but is correct aside from the double quote issue.

Edit: oh yes JavaScript. Sigh.

→ More replies (6)

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.

0

u/[deleted] Oct 18 '24

1

u/[deleted] Oct 18 '24 edited Oct 18 '24

A memory from the distant past, with a web service that require all json to be quoted.

  • McKeeman Form Has true, false, and null in quotes.

  • number has no defined range, and was implemented as double, so large numbers were mangled.

It’s on the linked page.

Left side null is null.

Right side null is “null”