0

I made a API system with flask-restplus and mongoengine

Also I'm new at mongodb and mongoengine.

I know that there is no foreign key in mongodb.

But after read mongoengine's docs, I found some similar fields.

ReferenceField is made fields that has relationship like RDB.

So I wonder that, If I set some field to ReferenceField, it works like RDB's foreign key?

Look at the below code about db schema.

class User(Document):
    no = SequenceField()
    userid = StringField(unique=True, required=True)
    userpw = StringField(required=True)

class Article(Document):
    no = SequenceField()
    subject = StringField(required=True)
    content = StringField(required=True)
    userid = ReferenceField(User, required=True, dbref=True)

When userid john is deleted in User schema, john's every article have to deleted.

Also every Article's userid have to already contain in User schema.

I know it is to easy if I use RDB foreign key and cascade option.

But I wonder that, if I use mongoengine's ReferenceField, it can be possible?

Or, What is the exact work ReferenceField do?

Thanks.

Hide
  • 3,199
  • 7
  • 41
  • 83
  • 1
    Please don't use that. It's using `DBRef`, a bad and whilst not "officially" deprecated, it's an old idea used to "emulate" the effects of a join. Modern MongoDB releases have `$lookup` where you need to actually join, and this does not support `DBRef` at all. MongoEngine itself is kind of lacking on maintenance anyway if not completely abandoned yet. So be wary – Neil Lunn Nov 06 '18 at 07:58
  • @NeilLunn Is there any active maintaining mongodb library? – Hide Nov 06 '18 at 07:59
  • 1
    MongoDB? Yes lots. But [MongoEngine](https://github.com/MongoEngine/mongoengine) is not a MongoDB product. There are "recent" changes, but AFAIK there have been very little over the past few years and their current version support is lagging behind. – Neil Lunn Nov 06 '18 at 08:05
  • @NeilLunn I got it. But my company already decided to use mongoengine, so I have to use it. – Hide Nov 06 '18 at 09:51

0 Answers0