Search code examples
mongodbmeteor

How can I get all of fields with meteor and mongodb?


Here is what Users.find({_id: userId}) looks like on Users collection

{
  "_id" : "123sdasdasd",
  "createdAt" : ISODate("2022-06-15T20:36:01.549+0000"),
  "roles" : {
      "internal" : [
          "admin",
          "maneger"
      ],
      "client" : [
          "admin",
          "maneger",
          "normal-user",
      ],
      "other" : [
          "admin",
          "maneger",
          "normal-user",
          "manage"
      ],
  },
  "username" : "sample@gmail.com"
}

And what I like to do is get all of the fields? on roles

so internal, client and other. I'd like to have them in an array ['internal', 'client', 'other'] like this.

const boardNames = ['internal', 'client', 'other']

I'm trying to do this on client side but if it's easier I can do on server side as well.

Can I do it?


Solution

  • I think both Ricardo and Klaucode are correct.

    const boardNames = Object.keys(user.roles)

    If you need also iterate over each kind of roles, you can use also "for of" syntax. for(const kind of roles) { console.log(kind); }