3

I have collections in MongoDB with name has Year

for example I have three collections

2015-Sam

2015-John

2014-Sam

How to get a list of

2015-Sam

2015-John

using Mongodb query

styvane
  • 59,869
  • 19
  • 150
  • 156
dhyabi
  • 329
  • 2
  • 13

2 Answers2

4

Use the getCollectionNames method and filter them using a regular expression:

> db.getCollectionNames().filter(function (c) { return /^2015\-/.test(c); })
[ "2015-foo", "2015-foo1" ]
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
0

If you're using MongoDB 3 or up, you can use db.listCollections with a filter on the collection's name. If you're using 2.x, then you would have to filter on the complete list as Ionica pointed out.

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82