Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Created August 25, 2011 15:20
Show Gist options
  • Save hastebrot/1170907 to your computer and use it in GitHub Desktop.
Save hastebrot/1170907 to your computer and use it in GitHub Desktop.
Example of DBRef support in Mongoose 2.0
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
mongoose.connect("127.0.0.1", "mongoose_dbref", 27017);
var PersonSchema = new Schema({
name : String
, age : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : { type: Schema.ObjectId, ref: 'Person' }
, title : String
, fans : [{ type: Schema.ObjectId, ref: 'Person' }]
});
var Story = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
var aaron = new Person({name: 'Aaron', age: 100});
aaron.save(function (err) {
if (err) throw err;
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: aaron._id
});
story1.save(function (err) {
if (err) throw err;
Person.findOne({name: "Aaron"}).populate('stories')
.run(function (err, person) {
if (err) throw err;
console.log("person =", person);
console.log("person.stories =", person.stories[0]);
})
Story.findOne({title: /Nintendo/i}).populate('_creator')
.run(function (err, story) {
if (err) throw err;
console.log("story =", story);
})
});
});
// person = { stories: [ ],
// _id: 4e566b7131ca6f2825000001,
// age: 100,
// name: 'Aaron' }
// person.stories = undefined
// story = { fans: [ ],
// _id: 4e566b7131ca6f2825000002,
// _creator:
// { stories: [ ],
// _id: 4e566b7131ca6f2825000001,
// age: 100,
// name: 'Aaron' },
// title: 'A man who cooked Nintendo' }
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
mongoose.connect("127.0.0.1", "mongoose_dbref", 27017);
var PersonSchema = new Schema({
name : String
, age : Number
, stories : [{ type: Schema.ObjectId, ref: 'Story' }]
});
var StorySchema = new Schema({
_creator : { type: Schema.ObjectId, ref: 'Person' }
, title : String
, fans : [{ type: Schema.ObjectId, ref: 'Person' }]
});
var Story = mongoose.model('Story', StorySchema);
var Person = mongoose.model('Person', PersonSchema);
var aaron = new Person({name: 'Aaron', age: 100});
aaron.save(function (err) {
if (err) throw err;
var story1 = new Story({
title: "A man who cooked Nintendo"
, _creator: aaron._id
});
story1.save(function (err) {
if (err) throw err;
aaron.stories.push(story1._id);
aaron.save(function (err) {
if (err) throw err;
Person.findOne({name: "Aaron"}).populate('stories')
.run(function (err, person) {
if (err) throw err;
console.log("person =", person);
console.log("person.stories =", person.stories[0]);
})
Story.findOne({title: /Nintendo/i}).populate('_creator')
.run(function (err, story) {
if (err) throw err;
console.log("story =", story);
});
});
});
});
// person = { stories: [ [object Object] ],
// name: 'Aaron',
// age: 100,
// _id: 4e56698f15dff83410000001 }
// person.stories = { fans: [ ],
// _id: 4e56698f15dff83410000002,
// _creator: 4e56698f15dff83410000001,
// title: 'A man who cooked Nintendo' }
// story = { fans: [ ],
// _id: 4e56698f15dff83410000002,
// _creator:
// { stories: [ 4e56698f15dff83410000002 ],
// name: 'Aaron',
// age: 100,
// _id: 4e56698f15dff83410000001 },
// title: 'A man who cooked Nintendo' }
@suissa
Copy link

suissa commented Nov 26, 2013

mongoose-dbref|⇒ node example02.js

/Users/jeancarlonascimento/www/estudos/node_modules/mongoose/lib/utils.js:413
throw err;
^
TypeError: Object # has no method 'run'

@suissa
Copy link

suissa commented Nov 26, 2013

With exec runs

@yuchienho
Copy link

mongoose does not support multi-collection reference

@nil2854
Copy link

nil2854 commented Feb 25, 2016

its verry needfull example for referencing schema objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment