Json 如何在mongoose中编写键值对对象模式
我已经尝试过创建这样的模式,但还没有找到解决方案 我试着做这样的事Json 如何在mongoose中编写键值对对象模式,json,database,mongodb,object,mongoose,Json,Database,Mongodb,Object,Mongoose,我已经尝试过创建这样的模式,但还没有找到解决方案 我试着做这样的事 questions: { type: new Schema({ questionId: { type: String, }, correct: { type: Boolean, }, }), }, 我需要的输出是: questions: { [questionId1]:[boolean], [questionId2]:
questions: {
type: new Schema({
questionId: {
type: String,
},
correct: {
type: Boolean,
},
}),
},
我需要的输出是:
questions: {
[questionId1]:[boolean],
[questionId2]:[boolean]
}
例如:
questions:{
23:true,
29:false
}
提前感谢。如saksh73所指出的,可以使用Map完成模式()
可以使用以下方法添加布尔键值对:
const abc = new Schema({
questionId: {
type: Map,
of:Boolean
},
});
如果你想要一个对象而不是布尔值…你可以这样做:
const abc = new Schema({
questionId: {
type: Map,
of:new Schema({
id: Number,
title: String,
}),
},
});
我想我需要使用type:Map,但不知道如何在我的案例中使用它。
const abc = new Schema({
questionId: {
type: Map,
of:new Schema({
id: Number,
title: String,
}),
},
});