LearnBoost/mongoose — pull request #585 by pmlopes (2011-10-28 10:04:58+00:00)
Title: Suggestion for single embedded document support

Hi,

One of my challenges with mongoose is to have a single embedded doc in a doc, this patch allows this kind of schema declaration:

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/mongoose');

var PersonSchema = new Schema({
FirstName: {type: String, required: true},
LastName: String
});

var BlogPostSchema = new Schema({
creator: PersonSchema
});

/**
* Define model.
*/

var BlogPost = mongoose.model('BlogPost', BlogPostSchema);
var Person = mongoose.model('Person', PersonSchema);

var person = new Person({FirstName: 'Paulo', LastName: 'Lopes'});

var post = new BlogPost({title: 'test', creator: person});

post.save(function(err) {
if (err) return console.log(err);
BlogPost.find({}, function(err, docs) {
if(err) return console.log(err);
console.log(docs[0].creator);
docs[0].creator = null;
docs[0].save(function(err) {
if(err) return console.log(err);
mongoose.disconnect();
});
});
});

I don't know much about the internals of mongoose but this seems to work for me... could someone review and give some feedback? Thanks