Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created February 22, 2013 16:47
Show Gist options
  • Save aheckmann/5014821 to your computer and use it in GitHub Desktop.
Save aheckmann/5014821 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
var dbname = 'testing_subpopdoc';
console.log('dbname: %s', dbname);
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var schema = Schema({ _id: 'string', name: 'string' });
var A = mongoose.model('A', schema);
var BrandSchema = new Schema({
title: String,
description: String,
logo: [{name: String, path: String, src: { type: String, ref: 'A' }}], // <===
owner: {type: Schema.Types.ObjectId, ref: 'User'}
})
var Brand = mongoose.model('Brand', BrandSchema);
mongoose.connection.on('open', function () {
A.create({ _id: 'first', name: 'subpopdoc' }, function (err, doc) {
if (err) return done(err);
Brand.create({ logo: [{ name:'logo1', path: '.', src: doc._id }]}, function (err, brand) {
if (err) return done(err);
console.log('created brand', brand);
Brand.find().populate('logo.src', 'src').exec(function (err, docs) {
if (err) return done(err);
console.log('found', docs[0].logo);
done();
});
});
})
});
function done (err) {
if (err) console.error(err.stack);
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment