2016-10-08 3 views
0

간단한 픽스이지만 확실한 중첩 된 객체 구문을 얻으려고 노력하고 있습니다. MongoDB 문서를 만드는 데 사용하려고합니다.중첩 된 객체에서 MongoDB 문서 만들기

Mongo 문서는 두 명의 사용자간에 대화를 저장합니다. 대화의 각 메시지는 별도의 MongoDB 문서에 저장되며 대화 문서는 해당 메시지에 속한 각 메시지를 참조합니다.

여기에 (나는 OK라고 생각합니다) 대화 스키마

var ConversationSchema = new mongoose.Schema({ 
    participants: [ 
    { 
     user1: { 
     id: { 
       type: mongoose.Schema.Types.ObjectId, 
       ref: "User" 
      }, 
     username: String 
     }, 
     user2: { 
     id: { 
       type: mongoose.Schema.Types.ObjectId, 
       ref: "User" 
      }, 
     username: String 
     }, 
    }, 
    ], 
    started: Number, 
    messages: [ 
    { 
     type: mongoose.Schema.Types.ObjectId, 
     ref: "Message" 
    } 
    ] 
}); 

있어 그리고 여기 MongoDB를로 전달하는 객체를 생성에 나의 많은 시도 중 하나입니다.

var conv = { 
       participants : { 
       "participants.user1.id" : req.body.senderId, 
       "participants.user1.username" : req.body.senderName, 
       "participants.user2.id" : req.body.recipientId, 
       "participants.user2.username" : req.body.recipientName 
       }, 
       created : Date.now(), 
       messages : [] // The message _id is pushed in later. 
      } 

정말 '나를 위로하는'참가자 '비트입니다. 이 데이터는 클라이언트에서 되돌려지고 있지만, 내 var conv으로 가져올 수는 없습니다. 여기에 필요한 중첩 된 객체를 만드는 올바른 구문은 무엇입니까?

모든 안내가 멋져요! 고마워요 !!

답변

0

수정 됨! 네, 단순한 문법 오류였습니다. 다른 누군가가 여기에서 끝나면 올바른 형태가됩니다.

var conv = { 
       participants : { 
       "user1" : { 
        "id" : req.body.senderId, 
        "username" : req.body.senderName 
       }, 
       "user2" : { 
        "id" : req.body.recipientId, 
        "username" : req.body.recipientName 
       } 
       }, 
       created : Date.now(), 
       messages : [] // The message _id is pushed in later. 
      } 

또한 팁을 버리고 세탁하십시오. 당신이 그들에게 돌아올 때 상황은 훨씬 더 명확해질 것입니다.

관련 문제