Wie kann ich die hinzufügen/aktualisieren " ObjectId array in eine collection mit MongoDB(Mungo)?

Dies ist, Was ich will, als ein endgültiges Ergebnis. Ich habe keine Ahnung wie die update-array von Indizes.

Wie kann ich die hinzufügen/aktualisieren

meinem Schema gebaut mit Mungo

var postSchema  = new Schema({
    title: {type:String},
    content: {type:String},
    user:{type:Schema.ObjectId},
    commentId:[{type:Schema.ObjectId, ref:'Comment'}],
    created:{type:Date, default:Date.now}
});


var commentSchema  = new Schema({
    content: {type:String},
    user: {type:Schema.ObjectId},
    post: {type:Schema.ObjectId, ref:'Post'}
    created:{type:Date, default:Date.now}
});

Meine Controller sind:

//api/posts/
exports.postPosts = function(req,res){
    var post = new Post({
        title: req.body.title,
        content: req.body.content,
        user: req.user._id
    });
    post.save(function(err){
        if(err){res.send(err);}
        res.json({status:'done'});
    });
};


//api/posts/:postId/comments
exports.postComment = function(req,res){
    var comment = new Comment({
        content: req.body.content,
        post: req.params.postId,
        user: req.user._id
    });
    comment.save(function(err){
        if(err){res.send(err);}
        res.json({status:'done'});
    });
};

Brauche ich eine middleware? oder muss ich etwas tun im controller?

InformationsquelleAutor Keon Kim | 2015-05-22
Schreibe einen Kommentar