Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在子文档中填充模型实例数组?猫鼬_Javascript_Mongodb_Mongoose_Database Design_Schema - Fatal编程技术网

Javascript 如何在子文档中填充模型实例数组?猫鼬

Javascript 如何在子文档中填充模型实例数组?猫鼬,javascript,mongodb,mongoose,database-design,schema,Javascript,Mongodb,Mongoose,Database Design,Schema,我有一个嵌套为数组的子文档。在该子文档中,我引用了其他模型。使用.Find和.Populate方法,我可以接收子文档中引用的单个模型的整个对象(请参见下面的停止),但不能接收模型实例、事实/建议的数组。对于事实/建议,我收到一个对象ID数组。我可能只是拿着那些\u id再进行一次查询,但这看起来很混乱 有没有办法填充数组?我是否需要使用聚合+$lookup?是否有一种更为Mongo的方法来重组模式以简化此过程 谢谢你的帮助 我的子文档名为TourStop: const mongoose = re

我有一个嵌套为数组的子文档。在该子文档中,我引用了其他模型。使用
.Find
.Populate
方法,我可以接收子文档中引用的单个模型的整个对象(请参见下面的停止),但不能接收模型实例、事实/建议的数组。对于事实/建议,我收到一个对象ID数组。我可能只是拿着那些
\u id
再进行一次查询,但这看起来很混乱

有没有办法填充数组?我是否需要使用聚合+$lookup?是否有一种更为Mongo的方法来重组模式以简化此过程

谢谢你的帮助

我的子文档名为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;
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();
        })
    })
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
Schema,它填充得很好

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; 
而未正确填充的事实模式则返回一个带有_id的字符串数组

事实:

我正在运行一个测试,检查模式是否正确连接,以检索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;
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();
        })
    })

您可以使用以下代码来填充tours、stop、facts和Recommensions。 注意在
model
属性中,我们不应该给出字符串值,而应该给出模型本身。因此,您需要将它们导入到代码中

User.findOne({first_name:“Dean”})
.填充({
路径:“旅游”,
填充:{
路径:“停止,停止”,
型号:停止
}
})
.填充({
路径:“旅游”,
填充:{
路径:“tourStops.facts”,
模型:事实
}
})
.填充({
路径:“旅游”,
填充:{
路径:“tourStops.recommendations”,
模式:建议
}
})
。然后(用户=>{
console.log(用户);
});

您有5-6个模式,但只显示一个模式代码。我们如何以这种方式理解模式之间的关系?我将更新以包括其他相关模式!谢谢您的帮助。关于巡更模式呢?TourStop作为子文档嵌套在巡更中,我添加了巡更模式以反映。谢谢@SuleymanSah!这是工作!在这个特定的例子中,它似乎完成了任务,我只是在运行一个关联测试。但是,您认为通过.Aggregate&$lookup来实现这一点更好吗?对数据库执行3个单独的填充调用似乎很繁重。关于这个问题的任何意见???@ruskibenya另一个选择是使用如上所述的嵌套查找。既然你使用了填充词,我用填充词回答,你能考虑接受这个答案吗?