总是发现3级后猫鼬没有填充。 尝试使用mongoose@^4.12.4
var mongoose = require('mongoose');
var config = require('./config')
var Promise = require('bluebird')
mongoose.Promise = Promise
mongoose.connect(config.dbURL, config.mongo.options)
var NodeSchema = new mongoose.Schema({
friend: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Node'
},
name: String
});
var autoPopulateChildren = function(next) {
this.populate('friend');
next();
};
NodeSchema
.pre('findOne', autoPopulateChildren)
.pre('find', autoPopulateChildren)
var Node = mongoose.model('Node', NodeSchema)
var user1 = new Node({
name: 'A'
})
var user2 = new Node({
name: 'B'
})
var user3 = new Node({
name: 'C'
})
var user4 = new Node({
name: 'D'
})
var user5 = new Node({
name: 'E'
})
user1.friend = user2._id
user2.friend = user3._id
user3.friend = user4._id
user4.friend = user5._id
Node.remove({})
.then(Promise.all([user5, user4, user3, user2, user1].map(p => p.save())))
.then(_ => Node.findOne({
name: 'A'
}))
.then(r => console.log(r))

得到了以下.. 的 A - >乙 - > C - >对象
但预计以下...... 的 A - >乙 - > C - > d - &GT,E
请帮忙!.. 提前谢谢..
答案 0 :(得分:0)
user1.friend = user2
user2.friend = user3
user3.friend = user4
user4.friend = user5
Node.remove({})
.then(Promise.all([user5.save(), user4.save(), user3.save(), user2.save(), user1.save()]))
.then(() => Node.findOne({
name: 'A'
}))
.then(r => console.log(r))