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
}]
*/
I can't find it with populate, o.B is a objectId,o.B.name is undefined.
BSchema is a subdocument of ASchema, CSchema has a ref connection of BSchema.
I want to find all CSchema which CSchema's name is "c_xxx", and shows CSchema's B's name and its A's name.
Like this result: {CName:"c_xxx",BName:"b_xxx",AName:"a_xxx"}