1

I have 2 collections in my database

MongoDB Enterprise> show collections
mycoll1 
mycoll2

I want to pass these names to a variable say c1 and use values of this array in mongodb shell commands (here getting counts from collections). what I'm doing now is:

MongoDB Enterprise> for (var i in c1) { db.getCollection((c1[i])).find().count()}

but in output I'm getting only second collections count and not first one.

I'm new to javascrip: please suggest.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
SaP
  • 61
  • 1
  • 2
  • 10
  • `for ( var i of c1 )` `in` keys the "keys" of an object. Also see [`for..in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) and [`for..of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) for details on what the differences are. – Neil Lunn Jun 11 '18 at 08:50
  • you mean to say I need to use like this: for (var i of c1) { db.getCollection((c1[i])).find().count()} – SaP Jun 11 '18 at 08:56
  • `for (var i of db.getCollectionNames()) { print(db.getCollection(i).find().count()); }` – ippi Jun 11 '18 at 08:59
  • when I use for (var i in c1) { print(c1[i]) } it prints both values i.e. mycoll1 and mycoll2 but when I use for (var i of c1) { print(c1[i]) } it shows [unknown type] [unknown type] – SaP Jun 11 '18 at 09:06

0 Answers0