我知道MongoDB是无模式的.但是,我有一些对象/实体的数据模型,我有一组明确定义的属性,它们将包含它们.例如,如果我有一个学生实体,我知道它的json看起来像
{
name: "string",
id: "int",
class: "string"
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用MongoDB定义此架构?如果无法做到这一点,那么如何验证用户是否已发送完整集来创建新学生?
首先,我是Web框架的新手.我们正在使用Meteor.我有一个学生收藏:
Students = new Mongo.Collection('students');
Run Code Online (Sandbox Code Playgroud)
目前,我们将Rest API定义为:
// Maps to: /api/getAllStudents
Restivus.addRoute('getAllStudents', {authRequired: false}, {
get: function () {
var students = Students.find().fetch();
if (students.length > 0) {
return {status: 'success',count:students.length,data: students};
}
else {
return {
statusCode: 404,
body: {status: 'fail', message: 'Students not found'}
};
}}
});
};
Run Code Online (Sandbox Code Playgroud)
现在,可以有另一个API getStudentByName
// Maps to: /api/getStudentByName by name
Restivus.addRoute('getStudentByName', {authRequired: false}, {
get: function () {
var obj = {};
for(var key in this.queryParams){
var val = this.queryParams[key];
obj[key] = …Run Code Online (Sandbox Code Playgroud)