Ваша схема должна быть такой:
// Option : 1
const hotelesSchema = new Schema({
"_id" : Number, // This is optional now
"nombre": String,
"direccion": String,
"telefono": String,
"estrellas": String,
"precio": String,
"imagenes": String
})
/** Option : 2
(Or if you don't even need it at all -> _id will not be created automatically or even if you pass it won't be inserted)
const hotelesSchema = new Schema({
"nombre": String,
"direccion": String,
"telefono": String,
"estrellas": String,
"precio": String,
"imagenes": String
}, { _id: false }) */
const mainSchema = new Schema({
hoteles: [ hotelesSchema ]
}, { _id: false }); // Adding { _id: false } will not create _id on top level.
const MainColModel = mongoose.model('yourActualCollectionName', mainSchema, 'yourActualCollectionName');
Код:
const mainColObj = new MainColModel({
hoteles: [{
"_id": 123, // If you don't pass it here it won't be created.
"nombre": "Hotel Sonesta",
"direccion": "Cerritos",
"telefono": "3152020",
"estrellas": "5",
"precio": "850000",
"imagenes": "https://i.ibb.co/t2Hc8c4/Hotel-Sonesta.jpg"
},
{
"_id": 456,
"nombre": "Hotel Soratama",
"direccion": "Centro",
"telefono": "3204545",
"estrellas": "4",
"precio": "540000",
"imagenes": "https://i.ibb.co/vcyqQSf/Soratama.jpg"
}]
})
let dbResp = await mainColObj.save();
Результат:
{
"_id" : ObjectId("5e1bee2a39ae20e06dc116db"), // Won't be created if you add , { _id: false } to mainSchema
"hoteles" : [
{
"_id" : 123, // You don't see _id in Option : 2
"nombre" : "Hotel Sonesta",
"direccion" : "Cerritos",
"telefono" : "3152020",
"estrellas" : "5",
"precio" : "850000",
"imagenes" : "https://i.ibb.co/t2Hc8c4/Hotel-Sonesta.jpg"
},
{
"_id" : 456, // You don't see _id in Option : 2
"nombre" : "Hotel Soratama",
"direccion" : "Centro",
"telefono" : "3204545",
"estrellas" : "4",
"precio" : "540000",
"imagenes" : "https://i.ibb.co/vcyqQSf/Soratama.jpg"
}
],
"__v" : 0
}