Skip to content

Instantly share code, notes, and snippets.

@danmactough
Forked from aheckmann/index.js
Created September 26, 2011 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmactough/1241386 to your computer and use it in GitHub Desktop.
Save danmactough/1241386 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
mongoose.connect('localhost', 'testing_535');
console.error('mongoose version', mongoose.version);
var OID = mongoose.Types.ObjectId;
var ASchema = new mongoose.Schema({
square: mongoose.Schema.ObjectId
, task: Number
});
var A = mongoose.model('A', ASchema);
mongoose.connection.on('open', function () {
var id = new OID;
A.create(
{ square: id, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }
, { square: new OID, task: 1 }, function (err) {
if (err) return console.error(err.stack||err);
A.find({}, { square: 1 }, function (err, docs) {
if (err) return console.error(err.stack||err);
console.error(docs);
console.error('next');
var ids = id.toString();
A.find({ square: ids }, function (err, docs) {
if (err) return console.error(err.stack||err);
console.error('found by id');
console.error(docs);
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
});
});
})
});
/*
...
// Where is "square"?
[ { _id: 4e7fcfb76289dd601f00000f },
{ _id: 4e7fcfb76289dd601f000010 },
{ _id: 4e7fcfb76289dd601f000011 },
{ _id: 4e7fcfb76289dd601f000012 },
{ _id: 4e7fcfb76289dd601f000013 },
{ _id: 4e7fcfb76289dd601f000014 },
{ _id: 4e7fcfb76289dd601f000015 },
{ _id: 4e7fcfb76289dd601f000016 },
{ _id: 4e7fcfb76289dd601f000017 },
{ _id: 4e7fcfb76289dd601f000018 },
{ _id: 4e7fcfb76289dd601f000019 },
{ _id: 4e7fcfb76289dd601f00001a },
{ _id: 4e7fcfb76289dd601f00001b },
{ _id: 4e7fcfb76289dd601f00001c } ]
next
found by id
// All records returned instead of just the first!!
[ { square: 4e7fcfb76289dd601f000001, task: 1, _id: 4e7fcfb76289dd601f00000f },
{ square: 4e7fcfb76289dd601f000002, task: 1, _id: 4e7fcfb76289dd601f000010 },
{ square: 4e7fcfb76289dd601f000003, task: 1, _id: 4e7fcfb76289dd601f000011 },
{ square: 4e7fcfb76289dd601f000004, task: 1, _id: 4e7fcfb76289dd601f000012 },
{ square: 4e7fcfb76289dd601f000005, task: 1, _id: 4e7fcfb76289dd601f000013 },
{ square: 4e7fcfb76289dd601f000006, task: 1, _id: 4e7fcfb76289dd601f000014 },
{ square: 4e7fcfb76289dd601f000007, task: 1, _id: 4e7fcfb76289dd601f000015 },
{ square: 4e7fcfb76289dd601f000008, task: 1, _id: 4e7fcfb76289dd601f000016 },
{ square: 4e7fcfb76289dd601f000009, task: 1, _id: 4e7fcfb76289dd601f000017 },
{ square: 4e7fcfb76289dd601f00000a, task: 1, _id: 4e7fcfb76289dd601f000018 },
{ square: 4e7fcfb76289dd601f00000b, task: 1, _id: 4e7fcfb76289dd601f000019 },
{ square: 4e7fcfb76289dd601f00000c, task: 1, _id: 4e7fcfb76289dd601f00001a },
{ square: 4e7fcfb76289dd601f00000d, task: 1, _id: 4e7fcfb76289dd601f00001b },
{ square: 4e7fcfb76289dd601f00000e, task: 1, _id: 4e7fcfb76289dd601f00001c } ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment