我一直在尝试从给定架构插入文档,但无法通过REST Client插入它。代码的贴图如下:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var countries = require('../countries/countries.model');
var Employees = require('../Employees/Employees.model');
var countryModel = new countries();
var EmpModel = new Employees();
var City = new Schema({
name: String,
active: Boolean,
countryId: {type:Schema.ObjectId, ref:countryModel},
Employees: [{type: Schema.ObjectId, ref: EmpModel}]
});
我输入了各种风格的JSON但没有一个成功
{
"name": "Delhi NCR",
"active": "true",
"countryId": country[0]._id,
Employees: [employees[1]._id]
}
{
"name": "Delhi NCR",
"active": "true",
"countryId": "ObjectId(xxxxxxxxx)",
Employees: ["ObjectId(xxxxxxxxx)", "ObjectId(xxxxxxxxx)"]
}
{
"name": "Delhi NCR",
"active": "true",
"countryId": "56dcccdddddddddaaaaaaaaa",
Employees: ["56dcccdddddddddaaaaaaaaa", "56dcccdddddddddaaaaaaaaa"]
}
但它都不起作用。我应该输入什么JSON。
答案 0 :(得分:1)
在架构定义中引用模型的方式不正确。 ref
选项告诉Mongoose在填充期间使用哪个模型,在这种情况下,您使用实际模型对象而不是具有模型名称的字符串值:
var City = new Schema({
name: String,
active: Boolean,
countryId: {type:Schema.ObjectId, ref: "countries"},
Employees: [{type: Schema.ObjectId, ref: "Employees"}]
});
重新定义架构后,您可以将_id
模型中的文档Employees
作为字符串传递,因为Mongoose会自动将这些文档转换为ObjectIds。
"Employees": ["56dcccdddddddddaaaaaaaaa", "56dcccdddddddddaaaaaaaaa"]
答案 1 :(得分:0)
您正在为活动字段传递字符串,而不是在JSON中传递布尔值。
$('#calendar').fullCalendar({
eventLimit: 4, // Here
viewRender: function (view) {
// Your code mentioned in your question.
}
});