Skip to content

Instantly share code, notes, and snippets.

@kurokikaze
Created May 28, 2010 07:57
Show Gist options
  • Save kurokikaze/416898 to your computer and use it in GitHub Desktop.
Save kurokikaze/416898 to your computer and use it in GitHub Desktop.
Creating reference in node-mongodb-native
var mongo = require('../lib/mongodb'),
sys = require('sys');
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : mongo.Connection.DEFAULT_PORT;
sys.puts("Connecting to " + host + ":" + port);
var db = new mongo.Db('test', new mongo.Server(host, port, {}), {});
db.open(function(err, db) {
db.collection('players', function(err, collection) {
collection.findOne({"name":'kze'}, function(err, player){
/* db.dereference(player.tank, function(err, tank) {
sys.puts('Player ' + player.name + ' tank speed is ' + tank.speed);
});*/
collection.insert({'name':'dark', 'identity': new mongo.DBRef('players', player._id)}
, function(err) { sys.puts('Reference inserted: ' + JSON.stringify(err))});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment