//models/contents.js
var mongoose = require('mongoose');
module.exports = new mongoose.Schema({
//关联字段 - 内容分类的id
category:{
type:mongoose.Schema.Types.ObjectId,
ref:'Content'
},
title:String,
description:{
type:String,
default:''
},
content:{
type:String,
default:''
}
});
//schema/content.js
var mongoose = require('mongoose');
var contentsSchema = require('../schemas/contents');
module.exports = mongoose.model('Content',contentsSchema);
//routers/admin.js
router.post('/content/add',function(req,res){
console.log(req.body)
})
打印的不是id而是[object Object] 这是什么原因
{ category: '[object Object]',
title: 'aaa',
description: 'aaaa',
content: 'aaaa' }
黄舟2017-05-16 13:41:32
From the source, Mongoose simplifies manual reference operations in MongoDB through ref and population.
In Mongoose, Schema uses ref to associate collections:
1. When writing, you need to take care of the writing part yourself and write two collections separately;
2. When reading, you need to call population to simplify the related part. Please refer to the link below for details:
http://mongoosejs.com/docs/po...
You need to use populate in mongoose in your code to associate it with the relevant object_id.
For reference.
Love MongoDB! Have fun!