使用父嵌入id创建嵌入文档

时间:2017-01-17 05:30:15

标签: mongodb mongoose

我在我的架构中嵌入了文档

var option = new mongoose.Schema({
option: {type: String, required: true},
nextItem: {type: String, required: true}// here I want to add next embedded document(item) id
});
var item = new mongoose.Schema({
        name: {type: String, required: true},
        options: [option],
        createdAt: {type: Date, default: Date.now()},
        parentItem: {type: String, required: true}, // here I want to add previous embedded document(item) id
    });

    var category = new mongoose.Schema({
        title: {type: String, required: true},
        items: [item]
    });

根据用户选择的项目,我必须显示另一个项目。如何在mongodb上保存时获取下一个和以前的嵌入式文档ID

1 个答案:

答案 0 :(得分:0)

请参阅node.js文档中的population section:)

下面我修改了您的架构(您可能需要更正架构名称)

 try {
    if(!empty($_POST)){
        $key =array_keys($_POST)[0];
        $date_to_floor = $_POST[$key]["date_to_floor"];
        $user_id = $_POST[$key]["assigned"];
        $hours_to_complete = $_POST[$key]["estimate"];
        $final_data = $this->adjust_working_hours($date_to_floor,$user_id,$hours_to_complete);
        $object->start_datetime = new \DateTime(date('n/j/Y H:i',strtotime($final_data['start_date'])));
        $object->end_datetime = new \DateTime(date('n/j/Y H:i',strtotime($final_data['end_date'])));
        $object->hours_remaining = $final_data['hours_remaining'];
    }
    $object = $this->admin->create($object);
    .....rest of the code