У меня есть две схемы с именами HotelSchema
и PricelineSchema
:
// schemas.js
const mongoose = require("mongoose")
module.exports.HotelSchema = mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
index: true
},
accommodation_type: {
type: String,
enum: ["هتل", "هتل آپارتمان", "مهمانسرا"],
index: true
},
pricelines: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Priceline' }]
})
module.exports.PricelineSchema = mongoose.Schema({
source: {
type: String,
required: true,
index: true
},
price: {
type: String,
required: true,
index: true
},
hotel: { type: mongoose.Schema.Types.ObjectId, ref: "Hotel" }
})
и соответственно две модели с именами Hotel
и Priceline
:
// models.js
const mongoose = require("mongoose")
const {HotelSchema, PricelineSchema} = require("./schemas")
module.exports.Priceline = mongoose.model("Priceline", PricelineSchema)
module.exports.Hotel = mongoose.model("Hotel", HotelSchema)
рассказ Iесть конечная точка, в которой async.waterfall
используется:
const async = require("async")
const bodyParser = require("body-parser")
const app = express()
app.use(bodyParser.json())
const { Hotel, Priceline } = require("./models")
function buildPricelineMany (arr, hotel) {
const pricelines = []
for (let priceline of arr) {
pricelines.push(
new Priceline({
source: priceline.source,
price: priceline.price,
hotel: hotel._id
})
)
}
return pricelines
}
app.put("/hotels", (req, res) => {
async.waterfall(
[
function (callback) {
Hotel.findOne({ name: req.body.name })
.populate("pricelines")
.exec(function (err, hotel) {
if (err) return callback(hotel)
return callback(null, hotel)
})
},
function (hotel, callback) {
const pricelines = buildPricelineMany(req.body.pricelines, hotel)
Priceline.insertMany(pricelines)
.then(docs => callback(null, hotel, docs))
.catch(err => {
return callback(err)
})
}
],
function (error, hotel, pricelines) {
if (error) {
res.status(500).jsonp({ errors: [error] })
return
}
res.status(200).jsonp(hotel)
}
)
})
app.listen(8080, "localhost")
, когда я вызываю populate('pricelines')
, в результате получается пустой массив, и я не знаю почему?!
{
"pricelines": [],
"_id": "5b0569dc1b99f37385c4d975",
"name": "baz",
"accommodation_type": "هتل",
"__v": 0
}
То, как я создал отношение один-ко-многим, точно соответствует документу http://mongoosejs.com/docs/populate.html.
Вот моя коллекция:
> db.pricelines.find({})
{ "_id" : ObjectId("5b058bf9e87a0e7ae7f6bd47"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfae87a0e7ae7f6bd48"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfae87a0e7ae7f6bd49"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfae87a0e7ae7f6bd4a"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd4b"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd4c"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd4d"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd4e"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd4f"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd50"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
{ "_id" : ObjectId("5b058bfbe87a0e7ae7f6bd51"), "source" : "jabama", "price" : "1231231", "hotel" : ObjectId("5b058beee87a0e7ae7f6bd42"), "__v" : 0 }
>
> db.hotels.find({})
{ "_id" : ObjectId("5b058beee87a0e7ae7f6bd42"), "name" : "baz", "accommodation_type" : "هتل", "pricelines" : [ ], "__v" : 0 }
{ "_id" : ObjectId("5b058bf6e87a0e7ae7f6bd46"), "name" : "استقلال", "accommodation_type" : "هتل", "pricelines" : [ ], "__v" : 0 }