任何人都可以帮助我尝试在对象中插入数据,但不使用数组的情况。
我需要这样的输出
{"_id":{"$oid":"5bacbda18ffe1a2b4cb9b294"},
"type":{"name":"prudhvi",
"headings":["Abstract","Introduction","Models"]}}
但是我越来越像这样
{"_id":{"$oid":"5c52d7484c7644263cbc428a"},
"name":"prudhvi",
"headings":["Abstract","Introduction","Models"],
"__v":{"$numberInt":"0"}}
我这样写了我的收藏集
var articleTypeSchema = new mongoose.Schema({
type: { type: mongoose.Schema.Types.Object, ref: 'typeSchema' }
});
var typeSchema = {
name:String,
headings:[String],
};
var Collections = {
type: mongoose.model('article_types',typeSchema)
}
这是我写的后端
userRouter.post('/addarticletype',(req,res)=>{
Collections.type.create({name:req.body.type,headings:req.body.head},function(err,result){
if (err) return res.status(500).send("There was a problem adding the information to the database");
else
res.status(200).send(result);
console.log(result);
})
})
答案 0 :(得分:1)
您需要按照以下方式重写模型:
var typeSchema = new mongoose.Schema ({
name:String,
headings:[String],
});
var articleTypeSchema = new mongoose.Schema({
type: { type: mongoose.Schema.Types.Object, ref: 'typeSchema' }
});
答案 1 :(得分:1)
在模型中,将数据类型更改为JSON而不是String,然后在尝试创建新集合时
var typeSchema = {
type:JSON,
};
第2步:在创建集合时,为该密钥创建一个JSON对象。
userRouter.post('/addarticletype',(req,res)=>{
Collections.type.create({type:{name:req.body.type,headings:req.body.head}},function(err,result){
if (err)
return res.status(500).send("There was a problem adding the information to the database");
else
res.status(200).send(result);
console.log(result);
})
})
第3步:完成