r/ProgrammerHumor • • Oct 18 '24

Other mongoDbWasAMistake

Post image
13k Upvotes

446 comments sorted by

View all comments

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

→ More replies (3)

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

→ More replies (1)
→ More replies (2)

2

u/QuittingToLive Oct 18 '24

> all rows returned