A
A
Alexander2016-02-01 15:24:39
MongoDB
Alexander, 2016-02-01 15:24:39

How to organize relationships in Mongoose?

Here I have two models:

var PostSchema = new Schema({
  title: String,
  image: {
           type: Schema.Types.ObjectId,
           ref: 'Image'
        },
  content: String
});

var ImageSchema = new Schema({
  url: String
});

is it possible not to specify the required ObjectId, let's say I want the post to be without a picture and in the response I want to receive something like this JSON:
{
   "post":{
      _id: // id,
      title: //заголовок,
      image: null,
      content://контент
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Semak, 2017-02-05
@semakalexander

Can.

var PostSchema = new Schema({
  title: String,
  image: {
           type: Schema.Types.ObjectId,
           ref: 'Image',
           default: null
        },
  content: String
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question