8

I try to create mongoose schema containing array of objects. Schema looks like:

const mapSchema = new Schema({
  name: {
    type: String
  },
  description: {
    type: String
  },
  rate: {
    type: Number,
    default: 0
  },
  sumOfRates: {
    type: Number,
    default: 0
  },
  places: [
    {
      name: String,
      description: String,
      difficulty: { type: String, default: 0 },
      sumOfRates: { type: String, default: 0 },
      lat: Number,
      lng: Number
    }
  ]
}, {
  timestamps: true
})

Places is array of objects. I tried to create mongoose schema and use it as element of places array(places: [ref: 'placeSchema']).

const placeSchema = new Schema({
  name: {
    type: String
  },
  description: {
    type: String
  },
  difficulty: {
    type: Number,
    default: 0
  },
  lat: {
    type: Number
  },
  lng: {
    type: Number
  }
})

But I think it would create another, separeted document in mongo(places document). Does it? I try to avoid such situation To keep it in one document.Now(code pasted above), gives me an error, when i try to insert json in postman:

{
    "name": "ergergergg",
    "description": "rgergrg",
    "places": [
        {
            "name":"cvcv",
            "description":"rgergrg",
            "lat":233,
            "lng":232
        }]
}

Error:

ValidationError: places: Cast to Array failed for value "[object Object]" at path "places"

Why such error? How to fix this?

2 Answers 2

20

The appropriate way to go would be to use of a sub schema.

const subSchema = new Schema({
  name: {
    type: String,
  },
  description: {
    type: String,
  },
  difficulty: {
    type: Number,
    default: 0,
  },
  lat: {
    type: Number,
  },
  lng: {
    type: Number,
  },
});

const mapSchema = new Schema({
  name: {
    type: String,
  },
  description: {
    type: String,
  },
  rate: {
    type: Number,
    default: 0,
  },
  sumOfRates: {
    type: Number,
    default: 0,
  },
  places: [subSchema],
}, {
  timestamps: true
});

To insert a new data

mapSchema.insert({
   name: "ergergergg",
   description: "rgergrg",
   places: [
        {
            name: "cvcv",
            description: "rgergrg",
            lat: 233,
            lng: 232,
        },
        {
            name: "22",
            description: "erwer2",
            lat: 123,
            lng: 321,
        },
   ],
});
Sign up to request clarification or add additional context in comments.

2 Comments

This was a very informative answer. Thank you, it solved my problem mostly. I just had one question, how can I make the places array optional? Thank you.
@ThisIsNotAnId Did you ever figure this out?
3

To make the subSchema optional, you can maybe do something like this @DanMossa :

    pages: {
        type: [subSchema],
        required: false
    },

Comments

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.