2

This page in the documentation says you can access a collection using c[name] or c.name, where c is a Collection, but what exactly does a sub-collection mean? I couldn't find any use of the term in the mongodb docs.

What I'm assuming, is that it gets the value of each document at key name, wihtin the collection, and represents that as its own collection. Is this the case?

bigblind
  • 12,539
  • 14
  • 68
  • 123
  • yaps this part of the documentation looks strange since there is no such thing as subcollection in mongoDB. – nickmilon Aug 03 '15 at 14:55
  • Your interpretation can't be valid too since collections have no keys only documents within a collection do. I asked @jessejiryudavis for a clarification I am sure he will respond https://twitter.com/nickmilon/status/628219803329626113 – nickmilon Aug 03 '15 at 15:06
  • @nickmilon What I meant was that a sub-collection would be the collection obtained by looking up the given key in each document within the collection. So if my collection looked like this: `[{"foo":{"id","1"}}, {"foo":{"id":"2"}}]`, I'd expect its `foo` sub collection to look like this: `[{"id":"1"}, {"id": "2"}]`. – bigblind Aug 03 '15 at 15:10

1 Answers1

5

A subcollection is just a naming convention of using . in a collection name as a way to organize your collection names.

So with the following code:

client = pymongo.MongoClient()
db = client['mydb']
coll = db['test']
subcoll = coll['subtest']

subcoll is a collection with a name of test.subtest. There's no defined relationship between test and test.subtest, it's just naming.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471