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
  • 4,445
  • 3
  • 31
  • 44
Yumiko
  • 448
  • 5
  • 16
  • 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
  • 16,283
  • 6
  • 45
  • 66
5

@MasterAM is right, the other way could be

db["user-settings"].find()
Harpreet Singh
  • 2,651
  • 21
  • 31
  • 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