Skip to content

Instantly share code, notes, and snippets.

@joeytwiddle
Created August 1, 2013 08:49
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 joeytwiddle/6129653 to your computer and use it in GitHub Desktop.
Save joeytwiddle/6129653 to your computer and use it in GitHub Desktop.
A dirty patch to mongoose that allows a few deep-population queries to work, whilst breaking other things! Don't use it! :D
diff --git a/lib/model.js b/lib/model.js
index 53a21e1..ba76805 100644
--- a/lib/model.js
+++ b/lib/model.js
@@ -1944,6 +1944,13 @@ function populate (model, docs, options, cb) {
return cb();
}
+
+ // We may be asked to resolve path "comments._creator" but we are given the comments, so we only need to look into doc["_creator"], so let's drop the first part of the path. TOOD: Or should this be all but the last part?
+ var seekPath = path;
+ if (path.indexOf(".")>=0) {
+ seekPath = path.split(".").slice(1).join(".");
+ }
+
var rawIds = [];
var i, doc, id;
var len = docs.length;
@@ -1968,7 +1975,7 @@ function populate (model, docs, options, cb) {
}
if (!ret || Array.isArray(ret) && 0 === ret.length) {
- ret = utils.getValue(path, doc);
+ ret = utils.getValue(seekPath, doc);
}
if (ret) {
@@ -2261,6 +2269,7 @@ function assignRawDocsToIdStructure (rawIds, resultDocs, resultOrder, options, r
Model._getSchema = function _getSchema (path) {
var schema = this.schema
, pathschema = schema.path(path);
+ var db = this.db;
if (pathschema)
return pathschema;
@@ -2287,6 +2296,15 @@ Model._getSchema = function _getSchema (path) {
// Also we need to handle array.$.path since schema.path
// doesn't work for that.
if (p !== parts.length) {
+ if (!foundschema.schema) {
+ var nextModelName = foundschema.caster.options && foundschema.caster.options.ref;
+ if (nextModelName) {
+ console.info("Got (through array) nextModelName="+nextModelName);
+ var nextmodel = db.model(nextModelName);
+ foundschema = nextmodel;
+ }
+ }
if ('$' === parts[p]) {
// comments.$.comments.$.title
return search(parts.slice(p+1), foundschema.schema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment