Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Created August 25, 2011 15:20
Show Gist options
  • Select an option

  • Save hastebrot/1170907 to your computer and use it in GitHub Desktop.

Select an option

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' }

ghost commented Jul 28, 2013

Copy link
Copy Markdown

Great example, thank you !

@nekaab

nekaab commented Aug 9, 2013

Copy link
Copy Markdown

Unfortunately, this is not a DBRef example, just a reference. DBRefs have are an object with "$ref", "$id", "$db" as attributes.

For an example, see http://docs.mongodb.org/manual/reference/database-references/

@suissa

suissa commented Nov 26, 2013

Copy link
Copy Markdown

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

suissa commented Nov 26, 2013

Copy link
Copy Markdown

With exec runs

@yuchienho

Copy link
Copy Markdown

mongoose does not support multi-collection reference

@nil2854

nil2854 commented Feb 25, 2016

Copy link
Copy Markdown

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