Search code examples
node.jsmongodbsaveschemaassign

How can i insert data to a schema array in nodejs?


I have a schema as shown below and I was trying to insert a value to that schema but data is not being assigned. How we can assign data to a schema array in nodejs? building is my schema instance.

my schema is shown below

geoValues: [
        {
            latitude: Number,

            longitude: Number,

    }
    ],

I tried like below for assigning data to my schema

building. geoValues.latitude = latitude;
 building. geoValues.longitude = longitude;

Solution

  • Check whether your mongoose schema looks like this,

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const buildingSchema = new Schema({
      geoValues: [{
        latitude: Number,
        longitude: Number
      }]
    });