Node.js 填充()指定多个模型
我希望对Node.js 填充()指定多个模型,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我希望对find()请求执行populate(),但要指定多个模型,因此,如果在第一个指定模型上未找到匹配项,则填充将在第二个指定模型上执行 下面是我想做的一个例子: const userSch = new Schema({ username: { type: String, required: true, }, password: { type: String, required: true, }, mai
find()
请求执行populate()
,但要指定多个模型,因此,如果在第一个指定模型上未找到匹配项,则填充将在第二个指定模型上执行
下面是我想做的一个例子:
const userSch = new Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
mail: {
type: String,
required: true,
unique: true,
}
});
const botSch = new Schema({
username: {
type: String,
required: true,
},
token: {
type: String,
required: true,
unique: true,
},
owner: mongoose.Schema.Types.ObjectId
});
const nspSch = new Schema({
name: String,
channels: [channels],
users: [{
id: {
type: mongoose.Schema.Types.ObjectId,
},
bot: {
type: Boolean,
}
}],
});
const channels = new Schema({
name: String,
message: [{
user: {
type: Schema.Types.ObjectId,
},
content: String,
}],
});
const nsp = mongoose.model('namespace', nspSch);
const Auth = mongoose.model('users', userSch);
const BotAuth = mongoose.model('bots', botSch);
function returnMs(nspID, channelID, callback) {
nsp.findOne({'_id': nspID}).populate({
path: 'channels.message.user',
model: 'users' || 'bots',
select: '_id username',
})
.exec(function(err, r) {
...
})
}
如果有一个npm
软件包,或者一个解决方案,或者甚至是一个编码轨道,请分享它
谢谢你你找错东西了。请查看文档中的内容。您不能“有选择地”选择模型,但可以将不同类型的数据放在同一个集合中。您甚至可以使一个模型显示为多个。谢谢您的回答,但这与我的情况不符。为了更精确,我添加了代码的其他部分。实际上,对于
频道中的每一条消息
,都有一个项目:用户
已完成,这要归功于一个填充()
,它在模型/集合用户中搜索,但我希望能够在未找到任何事件(如果用户是机器人)的情况下指定另一个模型/集合机器人
.有些人就是不知道如何接受暗示。“不能”是一个不变的术语。我非常清楚地告诉过你,你不能做你认为你想做的事情,有了一些经验,我可以看到你真正“需要”做的事情,包括“鉴别器”和更改代码和存储,这样所有的东西实际上都在“一个集合”中,而不是直接使用不同的模型。所以不能做用户| |机器人
。但鉴别器所做的是让你拥有一个包含用户和机器人的事物模型(或你想叫它的任何东西),然后你就开始推广你,为了你的答案,我会这么做,这是一个遗憾,因为从性能的角度来看,它会更好。否则,是的,我知道用户| |机器人
无法工作,但它是为了向您展示我想做什么。谢谢,再见