1

I have 2 models in python eve say foo and bar,and in foo I have an array of objectIds referring to bars. Is there a clean way to do it in python-eve without defining the custom route in flash and run the query manually with mongo?

and if I force to do it in mongo what is the recommended way to communicate with mongo instance

MKoosej
  • 3,405
  • 3
  • 21
  • 29

1 Answers1

2

I am not sure I understand your question but, have you looked into the data_relation setting? See Embedded Resource Serialization. Quoting from the Limitations paragraph:

Currently we support embedding of documents by references located in any subdocuments (nested dicts and lists). For example, a query /invoices?/embedded={"user.friends":1} will return a document with user and all his friends embedded, but only if user is a subdocument and friends is a list of reference (it could be a list of dicts, nested dict, ect.). We do not support multiple layers embeddings. This feature is about serialization on GET requests. There’s no support for POST, PUT or PATCH of embedded documents.

UPDATED If you simply want to query for documents which are referencing documents in other collections something like this would work:

?where={"reference_field":"54e328ec537d3d20bbdf2ed5"}

That's assuming reference_field is either a list of ids (of type objectid) or a objectid. Also see this answer.

Hope this helps.

Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33
  • I implemented the many to many relationship by saving only the list of ids ,rather than embedding the document cause my documents are large. So you're mongo query would look like find( { _id : { $in : array } } – MKoosej May 06 '15 at 16:10
  • that's precisely how you're supposed to operate of course. Embedding is a read-only thing. You store list of ids. I updated the answer. – Nicola Iarocci May 06 '15 at 16:17