1

I've just started to do my first steps in MongoDB so please do excuse me if my question would sound a bit stupid for you. I really tried my best to google the answer but so far failed to find one.

I wanted to ask what could be the good practice to handle the following situation. For example, I have collection "companies" (means the list of legal entities). Each document in it would have the same common fields like "name_official", "name_short", "name_group", "name_parent", etc.. Field "name_parent" supposed to reference the parent company (different document) in the same collection. Since not all the companies in collection are supposed to have any parent company, logically this field should be "required: false". I understand that I can easily keep the value type of this field as string. But is it possible to assign an ObjectID type to this field which would reference to _id Field of the document that holds information about parent company?

Thanks in advance for your help!

/* Update */ After experimenting with different ways of populating my MongoDB (importing from csv, via "create new company" form in my app, and with the help of specially written "populatedb.js" module) I finally came to conclusion that such a schema (with cross-document referencing within the same collection) doesn't allow to leave "name_parent" field empty. Even if you explicitly define it "required: false". So, instruction below doesn't work. comp_ParentName: {type: Schema.Types.ObjectId, ref: 'company', required: false}

Please, any other advises on the best practices on how to handle this kind of relationships? Once again what I want to do is to reference Parent Company (one legal entity and one document) to Child Company (separate legal entity and separate document) in the same collection "companies".

Really need a help! Thanks in advance!

Maxmit
  • 13
  • 5

1 Answers1

0

so you have something like this? :-

/** Dependencies */
// Mongoose
const mongoose = require('mongoose')

/** Data Schema */
const CompaniesSchema = new mongoose.Schema({
  // Name Official
  name_official: { type: String, trim: true, required: true },
  // Name Short
  name_short: { type: String, trim: true, required: true },
  // Name Group
  name_group: { type: String, trim: true, required: true },
  // Name Parent
  name_parent: { type: mongoose.Schema.Types.ObjectId, ref: "Companies" }
})

/** Export */
module.exports = mongoose.model('Companies', CompaniesSchema)

Then it should already work

lala
  • 1,309
  • 9
  • 21
  • Thanks for your quick answer @lala. I'm currently experimenting with "companies" collections by importing it into MongoDB from csv file. The problem that I face by using this method is that once the "name_parent" in csv is empty MongoDB Compass refuses to import collection :( It happens when I explicitly ask it to save the field as ObjectID type. That was the reason of my question: I guessed that MongoDB doesn't support inter-document referencing within the same collection... – Maxmit Nov 10 '20 at 06:50
  • maybe this could help you [ref](https://stackoverflow.com/questions/24964914/can-a-mongo-model-self-reference) – lala Nov 10 '20 at 06:55
  • thanks @lala. You actually confirmed that such a referencing is possible. So it means that I have a problem with importing data from csv file. Somehow Compass doesn't want to proceed with empty fields for ObjectID data type in csv file... So it's another topic ;) – Maxmit Nov 10 '20 at 07:09
  • yup. Maybe you add another layer of service or util to check whether there's `ObjectId` or not before importing data into your `csv` file or somehow. – lala Nov 10 '20 at 07:13