Created
June 13, 2014 07:47
-
-
Save gclsoft/cb09057a27410bf0870c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASchema = new mongoose.Schema({ | |
name: String, | |
B: [BSchema] | |
}); | |
var BSchema = new Schema({ | |
name: String | |
}); | |
var CSchema = new Schema({ | |
name: String, | |
B: { | |
type: Schema.ObjectId, | |
ref: 'BSchema' | |
} | |
}); | |
//This is OK, but not one time mongoose search: | |
CSchema.find({ | |
name: 'C_xxx' | |
}).exec(function(err, docC) { | |
docC.forEach(function(o) { | |
var BId = o.B; | |
ASchema.findOne({ | |
'B._id': BId | |
}, { | |
'B.$': 1 | |
} | |
).exec(function(err, docA) { | |
var Bname = docA.B[0].name; | |
var Cname = docA.name; | |
}); | |
}); | |
}); | |
//wrong in populate: | |
CSchema.find({ | |
name: 'C_xxx' | |
}) | |
.populate('ASchema.B') | |
.exec(function(err, docC) { | |
docC.forEach(function(o) { | |
var Bname = o.B.name; | |
//ERROR:o.B is a objectId,o.B.name is undefined | |
//Aname can't find | |
}); | |
}); | |
/* | |
Data like this: | |
ASchema = { | |
name: "A_xxx", | |
B: [{ | |
_id: 1, | |
name: "B_xxx" | |
}, { | |
_id: 2, | |
name: "B_xxx" | |
}] | |
} | |
CSchema = [{ | |
name: "C_xxx", | |
B: 1 | |
}, { | |
name: "C_xxx", | |
B: 2 | |
}] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment