ivica
ivica

Reputation: 1428

Nested collection with variable name

I'm trying to insert a collection inside a collection. Parent collection has it's name, but sub-collection will have a user-defined name (from a POST request).

I've tried it like this:

newInsert = jotc.newCollection.insert({'some':'data'})

where jotc is the existing parent collection. Instead of replacing newCollection with a value from a variable with the same name, MongoDB created a collection just like that: jotc.newCollection

Mongo version: 2.4.6 PyMongo version: 2.6.2 Python: 2.7.5

Upvotes: 0

Views: 254

Answers (1)

ivica
ivica

Reputation: 1428

I should have tried a little bit more before posting the question. Solution is to access the 'parent' collection dictionary-style:

newInsert = jotc[newCollection].insert({'some':'data'})

and it works.

Upvotes: 1

Related Questions