1

I have a peer set up the collection's name to

UserSettings = new Mongo.Collection("user-settings");

When I tried to query in MongoDB console, I am not able to do

 db.user-settings.find()

i get this error :-

ReferenceError: settings is not defined

How should I query a collection's name with dash?

Thanks

Henke
  • 2,831
  • 2
  • 16
  • 26
Yumiko
  • 378
  • 3
  • 15
  • Does this answer your question? [How do I reference a javascript object property with a hyphen in it?](https://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – Henke Jan 29 '21 at 10:50

2 Answers2

6

This is because user-settings is not a valid identifier in JavaScript and as such cannot be used to access the field using the dot notation.

It is actually interpreted as 2 expressions with a minus (-) operator between them.

You can use db.getCollection('user-settings') to get it.

MasterAM
  • 15,644
  • 6
  • 43
  • 63
5

@MasterAM is right, the other way could be

db["user-settings"].find()
Harpreet Singh
  • 2,603
  • 19
  • 30
  • Yepp, or that (known as "bracket notation") :) BTW, consider using an underscore (`_`) instead of the dash to make it a valid identifier. – MasterAM May 03 '16 at 11:10