0

我在 mongodb 中遵循集合布局:

<bookname>.<category_name>

例子:

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

所有文档都存储在最后一个集合中(cats_and_dogs.black_cats,cats_and_dogs.gray_dogs),现在我如何从cat_and_dogs获取所有集合名称?

4

1 回答 1

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/

于 2013-09-16T18:32:18.020 回答