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
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
Use the getCollectionNames
method and filter
them using a regular expression:
> db.getCollectionNames().filter(function (c) { return /^2015\-/.test(c); })
[ "2015-foo", "2015-foo1" ]
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.