Python Mongodb
  • Mongodb কি ?
  • এনভার্নমেন্ট তৈরী :
  • mongodbCompass কি :
    • mongodb compass কানেক্ট এরর :
  • pymongo :
  • mongoengine
    • 01.Connecting With Database
    • 02 Create Document.ipynb
    • 03 Fields
    • 04 FieldValidation
    • 05.Document Relationship One To One
    • 06 Document Relationship One To Many
    • 07 Document Relationship Many To Many
  • MongoDB Atlas
    • Setup
Powered by GitBook
On this page
  • One TO Many Relationship
  • One TO Many Relationship Using Reference Field
  • One TO Many Relationship Using Reference Field Example2
  1. mongoengine

06 Document Relationship One To Many

from mongoengine import *
connect(db='RelationExample')

One TO Many Relationship

যখন EmbeddedDocument কোন ক্লাস এ ব্যবহার করবো তখন ঐক্লাসের অবজেক্ট save মেথড দ্বারা ডেটাবেজ এ সেভ হবে না আমরা শুধু মাত্র অবজেক্ট তৈরী করে তা অন্য একটি অবজেক্ট এর রেফারেন্স হিসাবে সেট করতে পারি।

class Book(EmbeddedDocument):
    title = StringField()
    pages = IntField()
    price = DecimalField()
    
class Publisher(Document):    
    name=StringField()
    email=EmailField()
    books = ListField(EmbeddedDocumentField(Book))
    
    
book1 = Book(title='Python Basic',pages=200,price=250.0)
book2 = Book(title='Python OOP',pages=250,price=265.0)
book3 = Book(title='Python Crud',pages=350,price=390.0)

publisher= Publisher(name='Panjery',email='panjery@gmail.com',books=[book1,book2,book3])
publisher.save()    

One TO Many Relationship Using Reference Field

class Book1(Document):
    title = StringField()
    pages = IntField()
    price = DecimalField()
    
    
class Publisher1(Document):    
    name=StringField()
    email=EmailField()
    books = ListField(ReferenceField(Book1))
    
    
book01 = Book1(title='Python Basic',pages=200,price=250.0).save()
book02 = Book1(title='Python OOP',pages=250,price=265.0).save()
book03 = Book1(title='Python Crud',pages=350,price=390.0).save()

publisher=Publisher1(name='Panjerry',email='olee.techs@gmail.com',books=[book01,book02,book03]).save()                    

One TO Many Relationship Using Reference Field Example2

class Book2(Document):
    title = StringField()
    pages = IntField()
    price = DecimalField()
    publisher=ReferenceField('Publisher2')
    
class Publisher2(Document):    
    name=StringField()
    email=EmailField()
    books = ListField(ReferenceField(Book2))
    
    
publisher2=Publisher2(name='Panjerry',email='olee.techs@gmail.com').save()
book201 = Book2(title='Python Basic',pages=200,price=250.0,publisher=publisher2)
book202 = Book2(title='Python OOP',pages=250,price=265.0,publisher=publisher2)
book203 = Book2(title='Python Crud',pages=350,price=390.0,publisher=publisher2)

book201.save()
book202.save()
book203.save()
book201.publisher.name
#output 
'Panjerry'
Previous05.Document Relationship One To OneNext07 Document Relationship Many To Many

Last updated 1 year ago