gpt4 book ai didi

node.js - 使用 populate 查询两个模式

转载 作者:可可西里 更新时间:2023-11-01 10:47:31 26 4
gpt4 key购买 nike

所以我有两个模式,一个 PoemRegistrations 和另一个 CompetitionResults 我想在一个页面中呈现这两种形式。

当我呈现大部分 PoemRegistrations 字段和一个 CompetitionResults 时,我试图填充 PoemRegistrations 以从 CompetitionResults winnersName 字段中提取数据,但我遇到了一些问题。

这是我到目前为止尝试过的方法。

架构

winnersName: [{ type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResults' }],

在我目前正在尝试的路线上

router.get('/dashboard/all-poems', ensureAuthenticated, (req, res) => {
PoemRegistrations.find({}).
populate('winnersName').
exec(function(err, poemRegistrations) {
res.render('dashboard/all-poems.hbs', {
pageTitle: 'All Poems',
poemRegistrations: poemRegistrations
});
});
});

我希望能够通过做理想地呈现 winnersName

{{#each poemRegistrations}}
<tr>
<td>{{winnersName}}</td>
</tr>
{{/each}}

我想要什么

我希望能够从另一个架构中拉入一个字段,将其从一个架构拉入另一个架构并呈现到页面。

我可以毫无问题地拉入 PoemRegistration 字段,我只是想让 winnersName 呈现。

这里的任何帮助都会很棒。

编辑 1

模式[诗歌注册]

const express = require('express');
const mongoose = require('mongoose');

var app = express();

if (app.get('env') === 'production') {
mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
} else {
mongoose.connect('mongodb://localhost/pol-development', { useMongoClient: true });
}

var db = mongoose.connection;
mongoose.Promise = global.Promise;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection has been established");
});

var PoemRegistrationsSchema = mongoose.Schema({
schoolName: String,
competitionDate: String,

// Poem 1
poem1AuthorName: String,
poem1Title: String,
poem1Url: String,
poem1AnthologyUrl: String,
poem1LinesCheck: Boolean,
poem1CenturyCheck: Boolean,
poem1FreeCheck: Boolean,

// Poem 2
poem2AuthorName: String,
poem2Title: String,
poem2Url: String,
poem2AnthologyUrl: String,
poem2LinesCheck: Boolean,
poem2CenturyCheck: Boolean,
poem2FreeCheck: Boolean,

// Poem 3
poem3AuthorName: String,
poem3Title: String,
poem3Url: String,
poem3AnthologyUrl: String,
poem3LinesCheck: Boolean,
poem3CenturyCheck: Boolean,
poem3FreeCheck: Boolean,

winnersName: [{ type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResults' }],

// admin fields
poemRegistrationRequiredDocuments: Boolean
});

var PoemRegistrations = module.exports = mongoose.model('PoemRegistrations', PoemRegistrationsSchema);

模式[比赛结果]

const express = require('express');
const mongoose = require('mongoose');

var app = express();

if (app.get('env') === 'production') {
mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
} else {
mongoose.connect('mongodb://localhost/pol-development', { useMongoClient: true });
}

var db = mongoose.connection;
mongoose.Promise = global.Promise;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection has been established");
});

var CompetitionResultsSchema = mongoose.Schema({
schoolName: String,
winnersName: String,
winnersGrade: String,
winnersAddress: String,
winnersCity: String,
winnersZip: String,
competitionDate: String,
winnersTelephone: String,
winnersParentName: String,
winnersParentTelephone: String,
winnersTShirtSize: String,
winnersAccommodation: Boolean,
winnersAccommodationComments: String,

// admin fields
winnersAttendedRehersal: Boolean,
winnersAttendedMainCompetition: Boolean,
winnersReleaseForm: Boolean,

runnerUpsName: String,
runnerUpsGrade: String,
runnerUpsAddress: String,
runnerUpsCity: String,
runnerUpsZip: String,
runnerUpsTelephone: String,
runnerUpsParentName: String,
runnerUpsParentTelephone: String,
runnerUpsTShirtSize: String,
runnerUpsAccommodation: Boolean,
runnerUpsAccommodationComments: String,

// admin comments
runnerUpsAttendedRehersal: Boolean,
runnerUpsAttendedMainCompetition: Boolean,
runnerUpsReleaseForm: Boolean
});

var CompetitionResults = module.exports = mongoose.model('CompetitionResults', CompetitionResultsSchema);

最佳答案

更好的方法是重新设计您的架构,如下所示:

var mongoose = require('mongoose'), 
Schema = mongoose.Schema;

var poemSchema = Schema({
authorName: String,
title: String,
url: String,
anthologyUrl: String,
linesCheck: Boolean,
centuryCheck: Boolean,
freeCheck: Boolean
});

var competitionResultSchema = Schema({
schoolName: String,
name: String,
grade: String,
address: String,
city: String,
zip: String,
competitionDate: String,
telephone: String,
parentName: String,
parentTelephone: String,
tShirtSize: String,
accommodation: Boolean,
accommodationComments: String,

// admin fields
attendedRehersal: Boolean,
attendedMainCompetition: Boolean,
releaseForm: Boolean,
});


var poemRegistrationSchema = mongoose.Schema({
schoolName: String,
competitionDate: String,

// poems fields
poems: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Poem' }],

// competition results fields
winner: { type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResult' },
runnerUp: { type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResult' },

// admin fields
poemRegistrationRequiredDocuments: Boolean
});


var CompetitionResults = mongoose.model('CompetitionResult', competitionResultSchema);
var Poem = mongoose.model('Poem', poemSchema);
var PoemRegistration = mongoose.model('PoemRegistration', poemRegistrationSchema);

当您创建poemRegistration 数据时,您还需要poemswinnerrunnerUp 数据。例如,当您使用异步库的 waterfall 创建 3 首诗时工作流程:

var  workflowPipeline = [
function(callback) {
Poem.insertMany([
{
authorName: "author1",
title: "foo1",
url: "foo/bar",
anthologyUrl: "foo/bar",
linesCheck: false,
centuryCheck: false,
freeCheck: false
},
{
authorName: "author2",
title: "foo2",
url: "foo/bar2",
anthologyUrl: "foo/bar2",
linesCheck: false,
centuryCheck: false,
freeCheck: false
},
{
authorName: "author3",
title: "foo3",
url: "foo/bar3",
anthologyUrl: "foo/bar3",
linesCheck: false,
centuryCheck: false,
freeCheck: false
}
], function(err, poems) {
if (err) {
callback(err, null);
} else {
callback(null, poems);
}
});
},
function(poems, callback) {
var winner = new CompetitionResult({ schoolName: 'School 1', name: 'Result 1' });
var runnerUp = new CompetitionResult({ schoolName: 'School 2', name: 'Result 2' });
callback(null, { poems: poems, winner: winner, runnerUp: runnerUp });
},
function(data, callback) {
var registration = new PoemRegistration({
schoolName: 'School 3',
competitionDate: "2018-02-14",
poems: data.poems.map(p => p._id),
winner: data.winner._id,
runnerUp: data.runnerUp._id
});

registration.save(function(err, reg){
if (err) return callback(err, null);
callback(null, reg);
});
}
];

async.waterfall(workflowPipeline, function (err, result) {
// result is the new poem registration
});

关于node.js - 使用 populate 查询两个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762832/

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