gpt4 book ai didi

javascript - Populate() 引用嵌套在对象数组中

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:30 25 4
gpt4 key购买 nike

我正在尝试使用 Show 模型中的数据填充()我的 User 模型中的所有订阅。我已经尝试过 .populate('subscriptions.show') 但它对结果没有任何作用。

如果我将订阅设置为像这样的普通引用数组

subscriptions: [{type: Schema.Types.ObjectId, ref: 'Show'}]

执行 populate('subscriptions') 按预期工作

我查看了在 Stackoverflow 上可以找到的每个类似问题以及在文档中可以找到的内容。我看不出我做错了什么。

我正在使用的完整测试文件源 https://gist.github.com/anonymous/b7b6d6752aabdd1f9b59

架构和模型

var userSchema = new Schema({
email: String,
displayName: String,
subscriptions: [{
show: {type: Schema.Types.ObjectId, ref: 'Show'},
favorite: {type: Boolean, default: false}
}]
});

var showSchema = new Schema({
title: String,
overview: String,
subscribers: [{type: Schema.Types.ObjectId, ref: 'User'}],
episodes: [{
title: String,
firstAired: Date
}]
});

var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);

初始数据

var user = new User({
email: "test@test.com",
displayName: "bill"
});

user.save(function(err, user) {
var show = new Show({
title: "Some Show",
overview: "A show about some stuff."
});

show.save();
user.subscriptions.push(show);
user.save();
});

查询

User.findOne({
displayName: 'bill'
})
.populate('subscriptions.show')
.exec(function(err, user) {
if (err) {
console.log(err);
}

console.log(user);
});

结果:

{
_id: 53a7a39d878a965c4de0b7f2,
email: 'test@test.com',
displayName: 'bill',
__v: 1,
subscriptions: [{
_id: 53a7a39d878a965c4de0b7f3,
favorite: false
}]
}

最佳答案

也许是一些代码,还有对您的方法的一些更正。您想要一种“manyToMany”类型的联接,可以按如下方式进行:

var async = require("async"),
mongoose = require("mongoose"),
Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/user');


var userSchema = new Schema({
email: String,
displayName: String,
subscriptions: [{ type: Schema.Types.ObjectId, ref: 'UserShow' }]
});

userShows = new Schema({
show: { type: Schema.Types.ObjectId, Ref: 'Show' },
favorite: { type: Boolean, default: false }
});

var showSchema = new Schema({
title: String,
overview: String,
subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
episodes: [{
title: String,
firstAired: Date
}]
});


var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);
var UserShow = mongoose.model('UserShow', userShows);

var user = new User({
email: 'test@test.com',
displayName: 'bill'
});

user.save(function(err,user) {

var show = new Show({
title: "Some Show",
overview: "A show about some stuff."
});

show.subscribers.push( user._id );
show.save(function(err,show) {
var userShow = new UserShow({ show: show._id });
user.subscriptions.push( userShow._id );
userShow.save(function(err,userShow) {
user.save(function(err,user) {
console.log( "done" );
User.findOne({ displayName: "bill" })
.populate("subscriptions").exec(function(err,user) {

async.forEach(user.subscriptions,function(subscription,callback) {
Show.populate(
subscription,
{ path: "show" },
function(err,output) {
if (err) throw err;
callback();
});

},function(err) {
console.log( JSON.stringify( user, undefined, 4) );
});


});
});
});
});

});

这应该显示一个填充的响应,如下所示:

{
"_id": "53a7b8e60462281231f2aa18",
"email": "test@test.com",
"displayName": "bill",
"__v": 1,
"subscriptions": [
{
"_id": "53a7b8e60462281231f2aa1a",
"show": {
"_id": "53a7b8e60462281231f2aa19",
"title": "Some Show",
"overview": "A show about some stuff.",
"__v": 0,
"episodes": [],
"subscribers": [
"53a7b8e60462281231f2aa18"
]
},
"__v": 0,
"favorite": false
}
]
}
<小时/>

或者没有“manyToMany”也可以。请注意,这里没有初始调用来填充:

var async = require("async"),
mongoose = require("mongoose"),
Schema = mongoose.Schema;


mongoose.connect('mongodb://localhost/user');


var userSchema = new Schema({
email: String,
displayName: String,
subscriptions: [{
show: {type: Schema.Types.ObjectId, ref: 'UserShow' },
favorite: { type: Boolean, default: false }
}]
});


var showSchema = new Schema({
title: String,
overview: String,
subscribers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
episodes: [{
title: String,
firstAired: Date
}]
});


var User = mongoose.model('User', userSchema);
var Show = mongoose.model('Show', showSchema);

var user = new User({
email: 'test@test.com',
displayName: 'bill'
});

user.save(function(err,user) {

var show = new Show({
title: "Some Show",
overview: "A show about some stuff."
});

show.subscribers.push( user._id );
show.save(function(err,show) {
user.subscriptions.push({ show: show._id });
user.save(function(err,user) {
console.log( "done" );
User.findOne({ displayName: "bill" }).exec(function(err,user) {

async.forEach(user.subscriptions,function(subscription,callback) {
Show.populate(
subscription,
{ path: "show" },
function(err,output) {
if (err) throw err;
callback();
});

},function(err) {
console.log( JSON.stringify( user, undefined, 4) );
});


});
});
});

});

关于javascript - Populate() 引用嵌套在对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358630/

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