2

Is this

blog: [{type: Schema.Types.ObjectId, ref: 'BlogEntry'}]

the same as this

blog: {type: [Schema.Types.ObjectId], ref: 'BlogEntry'}

or is the second example incorrect?

1
  • I found the second option useful because you can set a custom setter on the schema property with it. Exactly what I was looking for. Commented Mar 19, 2021 at 11:39

1 Answer 1

4

Both are correct, but they're not the same. In the first one, the key blog is an array of objects. Each object contains two keys: type and ref where type is a single ObjectId and ref is already define. In the second one the key blog is no longer an array but a single object. Now, in this object the key type is an array of ObjectId.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. Isn't the second example better then, as the 'ref' key is redundant to be present in every single object?
You're welcome and that depends of what you want for your application. But yes, with the second example you avoid the redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.