What is the difference between EmbeddedDocumentField and ReferenceField in mongoengine

前端 未结 2 2124

Internally, what are the differences between these two fields? What kind of schema do these fields map to in mongo? Also, how should documents with relations be added to the

2条回答
  •  醉梦人生
    2021-01-02 04:06

    EmbeddedDocumentField is just path of parent document like DictField and stored in one record with parent document in mongo.

    To save EmbeddedDocument just save parent document.

    >>> some_author = User.objects.get(name="ExampleUserName")
    >>> post = Post.objects.get(author=some_author)
    >>> post.comments
    []
    >>> comment = Comment(text="cool post", tag="django")
    >>> post.comment.append(comment)
    >>> post.save()
    
    >>> post.comment
    []
    
    >>> Post.objects.get(author=some_author).comment
    []
    

    See documentation: http://docs.mongoengine.org/guide/defining-documents.html#embedded-documents.

提交回复
热议问题