我正在设计一个 Web 应用程序,用于管理母公司和子公司的组织结构。有两种类型的公司:1-主公司,2-子公司。公司只能属于一个公司,但可以有几个子公司。我的猫鼬架构如下所示:
var companySchema = new mongoose.Schema({
companyName: {
type: String,
required: true
},
estimatedAnnualEarnings: {
type: Number,
required: true
},
companyChildren: [{type: mongoose.Schema.Types.ObjectId, ref: 'Company'}],
companyType: {type: String, enum: ['Main', 'Subsidiary']}
})
module.exports = mongoose.model('Company', companySchema);
Run Code Online (Sandbox Code Playgroud)
我将所有公司存储在一个集合中,每个公司都有一个数组,其中包含对其子公司的引用。然后我想将所有公司显示为一棵树(在客户端)。我想查询所有填充他们的孩子的主要公司,以及无限的嵌套级别。我怎样才能做到这一点?或者也许你知道更好的方法。我还需要能够查看、添加、编辑、删除任何公司。
现在我有这个:
router.get('/companies', function(req, res) {
Company.find({companyType: 'Main'}).populate({path: 'companyChildren'}).exec(function(err, list) {
if(err) {
console.log(err);
} else {
res.send(list);
}
})
});
Run Code Online (Sandbox Code Playgroud)
但它只填充一个嵌套级别。我感谢任何帮助
我想用 mongoose 作为 JSON 对象将一个对象填充到一个虚拟字段中,但它总是返回一个包含单个项目的数组。
这是我的方案代码(部分带有虚拟字段):
Order.virtual('client', {
type: 'ObjectId',
ref: 'User',
localField: 'clientId',
foreignField: '_id'
});
Run Code Online (Sandbox Code Playgroud)
这是我如何做人口:
Order.findOneAndUpdate({ internalStatus: 2}, { internalStatus: 3 })
.lean()
.populate({ path: 'client', select: 'email' })
.exec(function (err, order) {
//...
});
Run Code Online (Sandbox Code Playgroud)
这是我在返回的 JSON 中收到的内容:
{ _id: 5ad903d90443fe13b8c9061a,
client: [ { _id: 5b3755fe69635d1e942d00a8, email: 'user@user.com' } ] }
Run Code Online (Sandbox Code Playgroud)
这就是我想要实现的目标:
{ _id: 5ad903d90443fe13b8c9061a,
client: { _id: 5b3755fe69635d1e942d00a8, email: 'user@user.com' } }
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助或建议!
我有一个情况,我正在检查文档是否已存在,如果不存在,我将创建一个新文档。我需要填充文档内的 2 个字段。我的问题是.create方法不支持.populate方法,因为如果我尝试这样做,我会收到错误。此外,. populate方法也不适用于返回的文档。如何正确填充新创建的文档?这是我的代码:
Favorite.create({ user: req.user._id, dishes: req.params.dishId })
.then((favorite) => {
favorite.populate('user');
favorite.populate('dishes');
console.log('Favorite marked', favorite);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(favorite);
}, (err) => next(err))
}
})
.catch((err) => next(err));
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种使用猫鼬填充到别名/虚拟属性的方法?
就目前而言,猫鼬的种群是在引用属性中添加更多字段(通常是身份密钥)。我想保留原始属性,然后将引用的数据填充到其他属性。
例如:似乎我们需要更多的架构选项
profile_id: { type: mongoose.Schema.Types.ObjectId, ref: 'profiles', populateTo: 'profile' }
Run Code Online (Sandbox Code Playgroud)
结果返回应包含两个属性:profile_id和profile
经过搜索之后,我在github上发现了这个问题:( https://github.com/Automattic/mongoose/issues/3225
我有以下mongoose架构结构
userSchema = new Schema({
roles: [
role: {type: Schema.Types.ObjectId, ref: 'Role' }
]
})
rolesSchema = new Schema({
name: String,
roleEntities: [
{
entity : {type: Schema.Types.ObjectId, ref: 'RoleEntity' },
abilities : [{type: Schema.Types.ObjectId, ref: 'Ability' }]
}
]
}
roleEntitiesSchema = new Schema({
name: String
})
abilitiesSchema = new Schema({
name: String
})
Run Code Online (Sandbox Code Playgroud)
如何在USER模型上执行查找时填充所有这些嵌套文档?
我尝试使用populate如下
User.find(ctx.request.query).populate(
{path: 'roles.role'
,populate: { path: 'roleEntities.entity'}
}).
exec()
Run Code Online (Sandbox Code Playgroud)
但它并没有解决roleEntities.entity
项目组架构:
var ProjectGroupSchema = new Schema({
projectGroupId : String,
title : String
});
Run Code Online (Sandbox Code Playgroud)
项目架构:
var ProjectSchema = new Schema({
title : {type : String, default : '', required : true},
group : {type: String, ref: 'ProjectGroup' },
subscribers : [{type: String, ref: 'User' }]
});
Run Code Online (Sandbox Code Playgroud)
用户架构:
var UserSchema = new Schema({
userId : {type: String, require: true},
firstName : {type: String, required: true},
lastName : {type: String, required: true},
});
Run Code Online (Sandbox Code Playgroud)
然后,我可以进行以下填充:
project.findById(req.projectId})
.populate('subscribers')
.populate('group')
.exec(function(err, …Run Code Online (Sandbox Code Playgroud) 假设我们有Foo和 的模式Bar。Foo有一个字段bar。该字段可以包含引用文档的 ObjectId,Bar也可以包含不是 ObjectId 的其他任意对象。
const fooSchema = new mongoose.Schema({
bar: {
type: mongoose.Schema.Types.Mixed,
ref: 'Bar'
}
});
const Foo = <any>mongoose.model<any>('Foo', fooSchema);
const barSchema = new mongoose.Schema({
name: String
});
const Bar = <any>mongoose.model<any>('Bar', barSchema);
Run Code Online (Sandbox Code Playgroud)
现在假设我们有一堆Foo文件。
我希望能够populate在该bar领域使用猫鼬自动将Bar文档的引用替换为实际Bar文档本身。我还想保留所有其他不引用Bar文档的对象不变。
通常,我会使用这样的方法来获取所有Foo文档,然后填充该bar字段:
Foo.find().populate('bar')
Run Code Online (Sandbox Code Playgroud)
但是,此方法在遇到bar字段中不是 ObjectId 的对象时会抛出异常,而不是让它们保持原样。
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 ID:1):CastError:对于模型“Bar”的路径“_id”处的值“Some任意对象”,CastError:转换为 ObjectId 失败
我已经使用match …
Lemme花时间解释从开始到结束发生的事情.
序言:
用户a跟随其他10个人.当用户A登录时,来自10个人中的每个人的X个帖子被拉入视野中.
我不知道这是否是正确的做法,并会欣赏更好的方法.但是,我想尝试一下,它不起作用.
关注型号:
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let FollowSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
followers: [{
type: Schema.Types.ObjectId,
ref: 'Card'
}],
following: [{
type: Schema.Types.ObjectId,
ref: 'Card'
}]
});
module.exports = mongoose.model('Follow', FollowSchema);
Run Code Online (Sandbox Code Playgroud)
卡模型
let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let CardSchema = new Schema({
title: String,
content: String,
createdById: {
type: Schema.Types.ObjectId,
ref: 'User'
},
createdBy: {
type: String
}
});
module.exports = mongoose.model('Card', …Run Code Online (Sandbox Code Playgroud) 我正在构建一个应用程序,并且创建了 2 个模型。
const UserSchema = new Schema({
_id: Schema.Types.ObjectId,
account:{
type: String,
unique: true
},
email: String,
first_name: String,
last_name: String
}
const VenueSchema = new Schema({
_id: Schema.Types.ObjectId,
venue_type: String,
capacity: Number
})
Run Code Online (Sandbox Code Playgroud)
和
const MediatorSchema = new Schema({
_id: Schema.Types.ObjectId,
account:{
type: String,
unique: true
},
user: {type: Schema.Types.ObjectId,
ref:'User'
}
venue: {type: Schema.Types.ObjectId,
ref:'Venue'
}
})
Run Code Online (Sandbox Code Playgroud)
中介者模式的创建是为了填充多个路径。
问题是当我尝试创建一个查询时
var populateQuery = [{path:'user',match: { account:'testuser1'},select:'email'},{path:'venue',match: { venue_type: 'club'}, select:'venue_type'}];
const confirmedVenues = await Mediator.find({})
.exists('venue',true)
.populate(populateQuery)
.exec(); …Run Code Online (Sandbox Code Playgroud) 我先运行聚合管道,然后填充,然后尝试获取特定的数据模型,但现在还不够。
最后所需的结果如下:
[
{
_accountId: "5beee0966d17bc42501f1234",
name: "Company Name 1",
contactEmail: "email1@email.com",
contactName: "contact Name 1"
reason: "Warranties",
total: 1152,
lineItems: [
{
_id: "5beee0966d17bc42501f5086",
jobsiteAddress: "1234 Street Southwest Sunnyville, Wyoming 12345",
warrantyFee: 384
},
{
_id: "5bf43929e7179a56e21382bc",
jobsiteAddress: "1234 Street Southwest Sunnyville, Wyoming 12345",
warrantyFee: 384
},
{
_id: "5bf4392fe7179a56e21382bd",
jobsiteAddress: "1234 Street Southwest Sunnyville, Wyoming 12345",
warrantyFee: 384
}
]
},
{
_accountId: "5beee0966d17bc42501f1235",
name: "Company Name 2",
contactEmail: "email2@email.com",
contactName: "contact Name 2"
reason: "Warranties",
total: 1152, …Run Code Online (Sandbox Code Playgroud) mongoose mongodb mongodb-query aggregation-framework mongoose-populate