26

I want to use MongoEngine for my next project. Now I'm wondering whether I could also use PyMongo directly in the same project. Just for the case that I need something very special that is not supported directly via mongoengine.

Are there any doubts that this would work, or that I should not do that!?

Thomas Kremmel
  • 14,575
  • 26
  • 108
  • 177

1 Answers1

55

Author of MongoEngine here - MongoEngine is built upon pymongo so of course you can drop into pymongo - or use raw pymongo in your code!

There are some document helpers that allow you to access raw pymongo methods in MongoEngine eg:

class Person(Document):
    name = StringField()

# Access the pymongo collection for the Person document
collection = Person._get_collection()
collection.find_one()  # Use raw pymongo to query data
Community
  • 1
  • 1
Ross
  • 17,861
  • 2
  • 55
  • 73
  • Thanks. That is how I hoped it will work :) As I have the author here. I might ask another question :) In this thread one describes how to add a field to a collection: http://tiny.cc/xmofjw .. Has adding a field to a DynamicDocument, as described here: http://tiny.cc/4iofjw , the same effect (=updating the entire collection) or do I add the .tags (from the example) only to the document.. And somehow it would be great if I could define the type of the field that I m adding at runtime to the dynamicDocument. But maybe thats not the idea of mongoDB. Just starting and trying to understand. – Thomas Kremmel Aug 22 '12 at 08:33
  • Is including pymongo via raw only possible for queries? I tried to get db.your_collection.update({},{$set : {"new_field":1}},false,true) working, but no luck. – Thomas Kremmel Aug 22 '12 at 09:04
  • 1
    You could just define it in the document class and as long as its not required you wont have an issue. You can also do a pymongo update to set it - but if its not defined in the document then you wont see the value unless you're using a DynamicDocument – Ross Aug 22 '12 at 10:12
  • 1
    @Tom, no it's not just for queries - `update` should work fine. example: `User._get_collection().update({}, { '$set': { 'my_field': 1 } }, multi=True)` – philfreo Aug 06 '13 at 23:23
  • followup: Is there a good way to create the actual object from the results of a pymongo query? Just creating the Object with the result as param makes it unconnected – arikg Feb 18 '14 at 10:40
  • It would be good if this were documented in the mongoengine docs. – Rebs Mar 12 '14 at 03:27
  • @AdamGriffiths, is there actually a way to do that? As argkig described, I would like to do a regular pymongo query then construct a MongoEngine query object from it. – J-bob Aug 25 '14 at 16:15
  • Ok the. @Ross. Is this indeed possible? – J-bob Aug 27 '14 at 11:05
  • you can use `__raw__` to use pymongo syntax in a query or you can use `DocClass._from_son(raw_pymongo_dict)` to create a single instance of a document from raw pymongo dictionaries. – Ross Sep 01 '14 at 08:02
  • @Ross How can I create a new collection dynamically using pymongo from my mongoengine instance? like this `result = db[collection_name].aggregate(pipeline)` when using just pymongo? – magnoz Feb 13 '19 at 10:07