Adding field with relationship to another collection

We have a collection - Advertisement. Like to add an additional parameter - advertiserId.
Have another collection - deal which has advertiserId and advertisementId parameters.
Like to advertiserId to Advertisement based on the deal collection where the documents has both advertiserId and advertisementId.
How do we do this ?

Hi @prasad_mokkapati,

Welcome to MongoDB community.

Hopefully I understand your intentions correctly and you wish to populate two collection with documents that have “relationship” fields between them which is a normal design in MongoDB.

However, since MongoDB has a flexible schema we don’t enforce this relationships and application should either populate the documents correctly or use a $merge aggregation to copy data from one set of documents to another by specifying the merging conditions.

You can populate the collections during your application CRUD or via a script…

db.advertisment.update({_id : .... }, {$set : { advertiserId: "YYY" }})

db.deal.update({_id : .... }, {$set : { advertisementId : "XXX", "advertiserId : "YYY" }})

What you can restrict in MongoDB is the structure of you documents to enforce specific fields presence using the Json schema validation

Thanks
Pavel