I wanted to query all "doc" properties inside the "doc", like a nested. I guess you can understand further what I want to explain from the code below:
mongoose.connect("mongodb://127.0.0.1:27017/stationDB");
const docSchema = mongoose.Schema({
naturka: String,
address: String,
coefficient: String,
wheel: String,
weight: Number
})
const stockSchema = mongoose.Schema({
rollingStockNumber: String,
docs: [docSchema]
});
const Doc = mongoose.model('Doc', docSchema);
const Stock = mongoose.model('Stock', stockSchema);
let doc;
for(i=0; i<naturkaMinifiedArrayReady.length; i ){
const doc = new Doc({
naturka: naturkaMinifiedArrayReady[i],
address: arrayOfWayPlanForm[i],
coefficient: valueOfCarLength[i],
wheel: numberOfWheels[i],
weight: valueOfCarWeight[i]
});
docs = doc
};
const stock = new Stock({
rollingStockNumber: rollingStock,
docs: docs
});
stock.save((err) => {
if (err) {
res.redirect('/')
}
});
I have inserted 8 documents, but returning only the last document as you see:
{ _id: new ObjectId("62a1ac800d9d39966878ebb8"), rollingStockNumber: '922698012567076507200129101700057022060000000', docs: [ { naturka: '08 94331444 0271 030 72240 00300 0012 0 0 0 0 01/00 96 04 105 0200 OXP', address: 'Toshkent', coefficient: '1.05', wheel: '4', weight: 50, _id: new ObjectId("62a1ac800d9d39966878ebb7") } ], __v: 0 }
CodePudding user response:
The doc defined with let is an array, but you do =. If you add the data to the array with push, your problem will be solved.
let docs = [];
for(i=0; i<naturkaMinifiedArrayReady.length; i ){
const doc = new Doc({
naturka: naturkaMinifiedArrayReady[i],
address: arrayOfWayPlanForm[i],
coefficient: valueOfCarLength[i],
wheel: numberOfWheels[i],
weight: valueOfCarWeight[i]
});
docs.push(doc)
};