몽구스 스키마를 동적으로 생성하는 방법은 무엇입니까?
MongoDB와 mongoose로 node.js에서 작동하는 앱이 있습니다.내 앱은 단순히 양식 데이터를 전송/삭제/편집하며, 이를 위해 나는 다음과 같은 몽구스 모델을 가지고 있습니다.
var mongoose = require('mongoose');
module.exports = mongoose.model('appForm', {
User_id : {type: String},
LogTime : {type: String},
feeds : [
{
Name: {type: String},
Text : {type: String},
}
]
});
그리고 그것은 잘 작동합니다!
이제 사용자가 양식에 필드를 추가하고 텍스트를 입력한 후 게시할 수 있도록 양식에 기능을 추가하려고 합니다.클라이언트 측에서 동적 기능을 생성하는 것은 문제가 없지만 mongoose.model이 올바르게 구성되어야 한다는 것을 알고 있습니다.제 질문은 어떻게 변수 값(동적으로 생성된 형식 데이터 이름과 텍스트)을 mongoose 스키마에 추가할 수 있습니까?
를 사용하는 것이 보입니다.strict: false그리고.Schema.Types.Mixed충고합니다.하지만, 전 이해할 수가 없어요...내가 시도한 것:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var feedSchema = new Schema({strict:false});
module.exports = mongoose.model('appForm', feedSchema);
팁 있어요?잘 부탁드립니다!
적용:strict: false기존 스키마 정의에 대한 옵션을 두 번째 매개 변수로 제공합니다.Schema생성자:
var appFormSchema = new Schema({
User_id : {type: String},
LogTime : {type: String},
feeds : [new Schema({
Name: {type: String},
Text : {type: String}
}, {strict: false})
]
}, {strict: false});
module.exports = mongoose.model('appForm', appFormSchema);
당신이 떠나고 싶다면,feeds완전히 도식적이지 않기 때문에, 그것이 당신이 사용할 수 있는 곳입니다.Mixed:
var appFormSchema = new Schema({
User_id : {type: String},
LogTime : {type: String},
feeds : [Schema.Types.Mixed]
}, {strict: false});
module.exports = mongoose.model('appForm', appFormSchema);
언급URL : https://stackoverflow.com/questions/28166463/how-to-create-mongoose-schema-dynamically
'programing' 카테고리의 다른 글
| Oracle에서 보기를 참조하는 외부 키 (0) | 2023.07.13 |
|---|---|
| 그라들 사용법.자바와 그루비가 함께? (0) | 2023.07.13 |
| 탭으로 구분된 데이터 파일을 생성하기 위한 SQL Plus 설정 (0) | 2023.07.13 |
| Oracle 11g 서버에서 시작 링크가 작동하지 않음 (0) | 2023.07.08 |
| JavaScript:ASP.NET 코드백에서 Alert.Show(메시지) (0) | 2023.07.08 |