将一个string数组作为node.js和mongodb中的一个参数

一般来说,我对node.js和REST都很陌生。 我的模型有以下模式:

"properties": { "name": { "type": "string", "description": "student name" }, "family": { "type": "string", "description": "family name" }, "subjects": { "type": "array", "description": "list of subjects taken", "minItems": 1, "items": { "type": "string" }, "uniqueItems": true } 

前两个属性是直接的,因为它们是string。 但是我很困惑如何为subjects发布数组。 我已经编写了这样的模型:

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var StudentSchema = new Schema({ name: String, family: String, subject: [String] }); module.exports = mongoose.model('Student', StudentSchema); 

我不知道我是否正确。 当我试图使用POSTMAN POST时,它坚持logging,但我不知道它是否存储为一个数组或string只。 我如何validation? 如何添加一个validation,该数组的长度必须> 1持久?

首先是validation部分

 var StudentSchema = new Schema({ name: String, family: String, subject: { type: [String], validate: [arrayLengthGreaterOne, '{PATH} size has to be > 1'] } }); function arrayLengthGreaterOne(val) { return val.length > 1; } 

你什么意思是“不知道它是如何存储的?”

我只是通过在mongo本身db.find()来查找date,但是你的语法看起来很好,所以我猜它已经正确存储了。