0

I have follow collection layout in mongodb:

<bookname>.<category_name>

Example:

cats_and_dogs.white_cats
cats_and_dogs.black_cats
cats_and_dogs.gray_dogs
ducks.black_ducks
ducks.white_ducks

All documents are stored in last collections(cats_and_dogs.black_cats,cats_and_dogs.gray_dogs), Now how I can get all collection names from cats_and_dogs ?

Community
  • 1
  • 1
Jhon
  • 433
  • 4
  • 19

1 Answers1

5

It's simplest if you use "bookname" as your database name, and "category_name" as your collection name. Then, you can run a command to list the collections for that database (so, to list the "categories" in each "bookname") . In the shell:

> use cats_and_dogs
> show collections
white_cats
black_cats
gray_dogs
> db.gray_dogs.find()

Depending on the language driver you are using, there will probably be a command to return all the collections in a given database in a cursor. Also, there is a page in the MongoDB docs that discusses best practices for data modeling, and it's worth checking out: http://docs.mongodb.org/manual/core/data-modeling/

sfritter
  • 891
  • 6
  • 11
  • I need data from cats_and_dogs.* collections – Jhon Oct 04 '13 at 18:00
  • MongoDB by nature does not support multi-collection queries (http://stackoverflow.com/questions/6502541/mongodb-query-multiple-collections-at-once). You'll have to run a separate query for each collection. Or, you could structure your data differently. For example, you could have one "cats_and_dogs" collection, and use some "type" : "gray_dogs" field to distinguish the documents. – sfritter Oct 07 '13 at 17:56