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
  • 55,207
  • 16
  • 142
  • 150
dhyabi
  • 319
  • 1
  • 12

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
  • 102,012
  • 80
  • 271
  • 450
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
  • 19,821
  • 9
  • 55
  • 82