gpt4 book ai didi

javascript - 如何在子文档中填充模型实例数组? MongoDB Mongoose

转载 作者:行者123 更新时间:2023-12-02 22:06:23 25 4
gpt4 key购买 nike

我有一个嵌套为数组的子文档。在该子文档中,我引用了其他模型。使用 .Find.Populate 方法,我可以接收子文档中引用的单个模型的整个对象(查看下面的 Stop),但不能接收模型实例数组,事实/建议。对于事实/建议,我收到一个对象数组 _ids。我可能可以只获取这些 _ids 并进行另一个查询,但这看起来很困惑。

有办法填充数组吗?我需要使用 Aggregate + $lookup 吗?是否有更多 Mongo 方式来重组 Schema 以简化此操作?

感谢您提供的所有帮助!

我的子文档名为 TourStop:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const TourStopSchema = new Schema({
stop: {
type: Schema.Types.ObjectId,
ref: 'Stop',
required:[true, 'must have a Stop'],
},
mandatory: {
type:Boolean,
default: false
},
check_in_time: {
type: Number,
default: 0
},
order: {
type:Number,
required: [true, 'Must have an order']
},
facts: [{
type: Schema.Types.ObjectId,
ref: 'Fact'
}],
recommendations: [{
type: Schema.Types.ObjectId,
ref: 'Recommendation'
}]
});


module.exports = TourStopSchema;

TourStops 位于 Tour 内部:

const mongoose = require('mongoose');
const TourStopSchema = require('../subdocs/tour_stop');
const Schema = mongoose.Schema;

const tourSchema = new Schema({
name: {
type:String,
required:[true,'Name is required!'],
unique: true
},
city: {
type: String,
required: [true, 'City is required!']
},
tourStops:[TourStopSchema]
});

const Tour = mongoose.model('Tour', tourSchema);
module.exports = Tour;

Stop 填充得很好的架构。

const mongoose = require('mongoose');
const LocationSchema = require('../subdocs/location');
const ImageSchema = require('../subdocs/image');
const Schema = mongoose.Schema;

const stopSchema = new Schema({
name:{
type: String,
required:[true,'Must have a name!']
},
location:LocationSchema,
categories: {
type: [String],
default:[]
},
images:{
type:[ImageSchema],
default:[]
}
});

const Stop = mongoose.model('Stop', stopSchema);
module.exports = Stop;

事实架构未正确填充,而是返回带有 _ids 的字符串数组

事实:

const mongoose = require('mongoose');
const ImageSchema = require('../subdocs/image');
const Schema = mongoose.Schema;

const factSchema = new Schema({
stop: {
type: Schema.Types.ObjectId,
ref:'Stop',
required:[true, 'A Fact must have a Stop!'],
},
description: {
type: String,
required: [true,'A Fact must have a description!']
},
images: {
type:[ImageSchema],
default:[]
}
});

const Fact = mongoose.model('Fact', factSchema);
module.exports = Fact;

我正在运行一个测试来检查模式是否正确连接以检索 TourStop 的所有属性:

it('saves a full relation graph', (done) => {
User.findOne({ first_name: 'Dean' })
.populate({
// in that user find the tours property and load up all tours
path: 'tours',
// inside of all those tours, find the tourstops property and load all associated stops
populate: {
path: 'tour_stops.facts',
model: 'Fact'
},
populate: {
path: 'tour_stops.stop',
model: 'Stop'
}
})
// .populate('tours.tour_stops[0].facts')
.then((user) => {
// console.log(user.tours[0].tour_stops[0].stop);
console.log(user.tours[0].tour_stops[0]);
// console.log(Array.isArray(user.tours[0].tour_stops[0].facts))
assert(user.first_name === 'Dean' );
assert(user.tours.length === 1);
assert(user.tours[0].name === "Awesome New Tour");
assert(user.tours[0].tour_stops[0].stop.name === 'Jaffa Clock Tower');
// assert(user.tours[0])
// assert(user.blogPosts[0].title === 'JS is Great');
// assert(user.blogPosts[0].comments[0].content === 'Congrats on great post!' );
// assert(user.blogPosts[0].comments[0].user.name === 'Joe' )
done();
})
})

最佳答案

您可以使用以下代码来填充旅游、站点、事实和建议。请注意,在 model 属性中,我们不应该给出字符串值,而应该给出模型本身。因此您需要将它们导入到您的代码中。

  User.findOne({ first_name: "Dean" })
.populate({
path: "tours",
populate: {
path: "tourStops.stop",
model: Stop
}
})
.populate({
path: "tours",
populate: {
path: "tourStops.facts",
model: Fact
}
})
.populate({
path: "tours",
populate: {
path: "tourStops.recommendations",
model: Recommendation
}
})
.then(user => {
console.log(user);
});

关于javascript - 如何在子文档中填充模型实例数组? MongoDB Mongoose ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com