Home > Back-end >  saving array type value inside mongodb schema using node js
saving array type value inside mongodb schema using node js

Time:09-17

I am trying to put array data inside my schema but its not showing in my mongodb Schema

ownerSchema.js

var ownerSchema = Schema({
    ownerId   : String,
    fname     : String,
    lname     : String,
    shopPlace : { 
                  type: Schema.Types.ObjectId,
                  ref: 'Shop'
                },
    shopType    String
});
var Owner = mongoose.model('Owner', ownerSchema);

shopSchema.js

var shopSchema = Schema({
    _id       : String,
    shopName  : String,
    location  : String,
    startDate : Date,
    endDate   : Date
});
var Shop  = mongoose.model('Shop', shopSchema);

as I am sending array format data inside shopType but its not showing any data inside schema

as I am sending this from postman my format look like this

{
 "ownerId"   : "Own001",
 "fname"     : "Tom",
 "lname"     : "jerry",
 "shopPlace" : {
                 "shopName" : "Juice Center",
                 "location" : "Mumbai",
               },
 "shopType"  : ["Juice","vegetables"]
}

but when I am sending this data only ownerId, fname,lname, shopPlace showing inside schema my shopType data can't be seen

const addTask = async (req, res) => {
  const { shopName, location } = req.body.shopPlace;
  const shopDetails = new Shop({
      shopName,
      location,
    });
    await shopDetails.save();

    const { ownerId, fname, lname, shopType } = req.body;
    const ownerDetail = new Owner({
      ownerId,
      fname,
      lname,
      shopType,
      shopPlace: shopDetail._id,
    });
    await shopDetail.save();
};

I want to store shopType in array format but Its even not showing inside my schema

CodePudding user response:

change your schema like it

var ownerSchema = Schema({
    ownerId   : String,
    fname     : String,
    lname     : String,
    shopPlace : { 
                  type: Schema.Types.ObjectId,
                  ref: 'Shop'
                },
    shopType :   [{
    type: String
}]
});
var Owner = mongoose.model('Owner', ownerSchema);
  • Related