【发布时间】:2020-02-06 01:03:55
【问题描述】:
一开始的问题已经解决了,谢谢!
现在,每当我插入文档时,它都会像这样:
{
_id: "5e3b64b6655fc51454548421",
todos: [ ],
title: "title"
}
它应该看起来像这样,因为在架构中,“title”属性位于“todos”属性之上。
{
_id: "5e3b64b6655fc51454548421",
title: "title",
todos: [ ]
}
JavaScript
//project schema
const schema = mongoose.Schema({
title: String,
todos: []
});
const Project = mongoose.model('Project', schema);
//add a project
app.post('/addProject', (req, res) => {
const collection = db.collection('projects')
const proj = new Project({title: req.body.title});
collection.insertOne(proj, function(err, results) {
if (err){
console.log(err);
res.send('');
return
}
res.send(results.ops[0]) //retruns the new document
})
})
【问题讨论】:
标签: javascript node.js json mongodb express