2013-10-31 3 views
0

그래서 여러 명령을 명령 목록에 저장하는 방법을 알아 내려고했지만 시도한 모든 것이 제대로 작동하지 않았습니다. 이것은 내가 지금까지 설정할 수 있지만 저장할 때 정말 여러 명령이 있습니다Mongoose에서이 중첩 스키마를 어떻게 작동 시키나요?

"command_list" : [ "command" : { "action" : "goto", "target" : "http://www.google.com" },      
        "command" : { "action" : "goto", "target" : "http://www.cnn.com" } ] 

같은 것을 할 때, 그것은

"command_list" : [ { "action" : "goto,goto", "target" : "http://www.google.com,http://www.cnn.com" } ] 

의 형식으로 저장이 방법이다. 지금까지 내 app.js이

var configSample = new Configurations({ 
     command_list_size: request.body.command_list_size, 
     command_list: [ {action: request.body.action, target: request.body.target}] 
}); 

같은 데이터를 저장하고, 모델은 내가 중첩 조치가가는 것을 얻는 방법 그래서이

var mongoose = require("mongoose"); 

var command = mongoose.Schema({ 
    action: String, 
    target: String 
}); 

var configSchema = mongoose.Schema({ 
    command_list_size: Number, 
    command_list: [command] 
}); 


module.exports = mongoose.model('Configurations', configSchema); 

처럼 보인다? 감사!

답변

0

데이터를 서버로 보낼 때 바로 포장하지 않는 것 같습니다. 다음을 사용하는 경우 :

command_list: [ {action: request.body.action, target: request.body.target}] 

이것은 모든 작업을 포착하고 대상을 동일하게 처리합니다. 이미 중첩 된 문서를 사용하여 서버에 배열을 보내는 것이 좋습니다.

다른 옵션은 서버에서받은 요소를 추출하기 위해 데이터를 구문 분석하는 것이지만 처음부터 올바르게 패키징하는 것이 더 쉬울 것이라고 생각합니다.

// not certain the chaining will work like this, but you get the idea. It works 
// on the string values you'll receive 
var actions = response.body.action.split(','); 
var targets = response.body.target.split(','); 

// the Underscore library provides some good tools to manipulate what we have 
// combined the actions and targets arrays 
var combinedData = _.zip(actions, targets); 

// go through the combinedData array and create an object with the correct keys 
var commandList = _.map(combinedData, function(value) { 
    return _.object(["action", "target"], value) 
}); 

을 만들 수있는 더 좋은 방법이있을 수 있습니다 : 당신은 당신이 무엇을, 당신이 사항 String.split() 메소드를 사용하여 객체를 재 구축 할 수 분할하고 싶었다면

:

ADDITION 새로운 객체이지만 이것은 속임수입니다.

편집 : 나는 to refactor the above code here.

시도에 대해 질문을 만들어

관련 문제