具有子文档的复杂模式

时间:2014-12-31 05:07:27

标签: javascript node.js mongodb mongoose

更新:

目前我有这个架构:

var Schema = new schema ({
    name: String,
    type: { type: String },
    date: { type: Date },
    descrip: String,
});

但我使用此架构生成2个文档:

THE A TYPE ({
    name: 'A',
    type: 'TYPE_B',
    date: { type: Date },
    descrip: 'DESC_A',
});

THE B TYPE ({
    name: 'B',
    type: 'TYPE_B',
    date: { type: Date },
    descrip: 'DESC_B',
});

A和B类型中的名称,类型和描述总是相同的,唯一改变的是日期字段,所以我在想,我该如何改进呢?如何在同一模式中插入多个日期,而不是始终创建具有相同名称,类型和描述值的文档?

所以我试图在其他模式中创建一个模式,但我不知道这是否可行,是吗?

我是这样想的:

var mainSchema = new schema ({
    name: String,
    type: { type: String },
    date: [ dateSchema ],
    descrip: String,
});

var dateSchema = new Schema ({
    date: {
     type: Date
    }  
});

我想要的是创建两个mainSchema,键入a并键入b,并在...中插入日期...

我这样做对吗?我怎样才能实现目标?

我正在寻找一个完整的答案和一个很好的解释,这就是赏金的原因。我不接受+/-答案。

2 个答案:

答案 0 :(得分:6)

要创建具有多个日期的记录,您可以使用日期数组。

var mainSchema = new schema ({
name: String,
type: { type: String },
dates: [Date],
descrip: String,
});

文件如下

THE A TYPE ({
name: 'A',
type: 'TYPE_B',
dates: ["2014-01-22T14:56:59.301Z","2015-01-22T14:56:59.301Z"],
descrip: 'DESC_A',
});

THE B TYPE ({
name: 'B',
type: 'TYPE_B',
dates: ["2015-01-22T14:56:59.301Z","2014-01-22T14:56:59.301Z"],
descrip: 'DESC_B',
});

参考:http://mongoosejs.com/docs/schematypes.html

保存您可以使用的文档。

exports.save = function(req,res){
var test = new newSchema;  // new object for newSchema domain.
test.name= req.body.name;
test.type= req.body.type;
test.desc= req.body.desc;
if(req.body.date){
    req.body.forEach(function(date){  // For every element of date from client.
        test.date.push(date)  // This pushes each and every date given from the client into the date array.
    })
}
test.save(function (saveErr, saved) {  // Saves the new document into db.
    if (saveErr) {
        console.log(saveErr)
        return;
    }
    res.status(HttpStatus.OK).json(saved);
});
};

对于更新,您可以使用like。

exports.update = function(req,res){  
newSchema.findOne({name: 'B'},function(err,test){  // Finds a record with name:'B' and stores in test the same can be done for name:'A'.
    if(test){
        if(req.body.date){
            req.body.forEach(function(date){
                test.date.push(date)  // pushes dates into existing test document (update).
            })
        }
        test.save(function (saveErr, saved) {  // Saves (updates) the document.
            if (saveErr) {
                console.log(saveErr)
                return;
            }
            res.status(HttpStatus.OK).json(saved);
        });
    }
});
};

希望这有帮助。

答案 1 :(得分:1)

如果您的代码在一个部分中写下来,它将如下所示

var mainSchema = new schema ({
    name: String,
    type: { type: String },
    date: [ {date: { type: Date } } ],
    descrip: String,
});

我认为这是错的。此外,您不需要像这样创建new schema,您只需在代码http://www.w3schools.com/js/js_objects.asp中使用{}。我认为正确的代码必须如下所示:

var mainSchema = {
 name: string,
 date: {},
 descrip: string
}

然后进入' date'你的约会。祝你好运。

========================如何使用==================== ===

抱歉延误。您可以使用mainSchema.date.date_1 = date;,也可以将date: {},更改为date: [],,并根据您的需要使用mainSchema.date.push(date)

相关问题