猫鼬:如何在不填充第一级字段的情况下填充第二级深层人口?在mongodb中 [英] Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb

查看:101
本文介绍了猫鼬:如何在不填充第一级字段的情况下填充第二级深层人口?在mongodb中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的猫鼬图式:

var SchemaA = new Schema({
    field1: String,
 .......
 fieldB : { type: Schema.Types.ObjectId, ref: 'SchemaB' }
});

var SchemaB = new Schema({
    field1: String,
 .......
 fieldC : { type: Schema.Types.ObjectId, ref: 'SchemaC' }
});

var SchemaC = new Schema({
    field1: String,
 .......
 .......
 .......
});

当我使用查找查询访问schemaA时,我希望具有字段/属性 SchemaA与SchemaB和SchemaC一起使用的方式与我们在SQL数据库中应用联接操作的方式相同.

While i access schemaA using find query, i want to have fields/property of SchemaA along with SchemaB and SchemaC in the same way as we apply join operation in SQL database.

这是我的方法:

SchemaA.find({})
 .populate('fieldB')
 .exec(function (err, result){ 

       SchemaB.populate(result.fieldC,{path:'fieldB'},function(err, result){

    .............................
        });

}); 

上面的代码运行正常,但是问题是:

The above code is working perfectly, but the problem is:

  1. 我想通过SchemaA获得SchemaC的信息/属性/字段,并且我不想填充SchemaB的字段/属性.
  2. 不希望获取SchemaB属性的原因是,额外的填充会降低不必要的查询速度.

长话短说: 我想通过SchemaA填充SchemaC而不填充SchemaB.

Long story short: I want to populate SchemaC through SchemaA without populating SchemaB.

您能提出任何建议/方法吗?

Can you please suggest any way/approach?

推荐答案

作为mongodb的狂热爱好者,我建议您使用关系数据库来存储高度关系数据-这就是它的基础.当您必须执行3次以上的查询才能获得一个对象时,您将失去mongodb的所有优势.

As an avid mongodb fan, I suggest you use a relational database for highly relational data - that's what it's built for. You are losing all the benefits of mongodb when you have to perform 3+ queries to get a single object.

Buuuuuut ,我知道该评论将充耳不闻.最好的选择是尽可能地保持对性能的意识.第一步是将字段限制为所需的最小数量.即使使用基本查询和 any 数据库引擎,这也是一种好习惯-仅获取您需要的字段(例如,SELECT * FROM ===不好...请停止执行它!).您还可以尝试执行精益查询,以帮助节省猫鼬对数据进行的大量后期处理工作.我没有对此进行测试,但它应该可以工作...

Buuuuuut, I know that comment will fall on deaf ears. Your best bet is to be as conscious as you can about performance. Your first step is to limit the fields to the minimum required. This is just good practice even with basic queries and any database engine - only get the fields you need (eg. SELECT * FROM === bad... just stop doing it!). You can also try doing lean queries to help save a lot of post-processing work mongoose does with the data. I didn't test this, but it should work...

SchemaA.find({}, 'field1 fieldB', { lean: true })
.populate({
    name: 'fieldB',
    select: 'fieldC',
    options: { lean: true }
}).exec(function (err, result) {
    // not sure how you are populating "result" in your example, as it should be an array, 
    // but you said your code works... so I'll let you figure out what goes here.
});

此外,一种非常"mongo"的操作方式是将SchemaC中的引用保存回SchemaA.当我说"mongo"方法时,您必须摆脱多年有关关系数据查询的思考.即使需要双向引用和/或数据重复,也要尽一切努力在数据库上执行更少的查询.

Also, a very "mongo" way of doing what you want is to save a reference in SchemaC back to SchemaA. When I say "mongo" way of doing it, you have to break away from your years of thinking about relational data queries. Do whatever it takes to perform fewer queries on the database, even if it requires two-way references and/or data duplication.

例如,如果我有一个Book模式和Author模式,则可能会将作者的名字和姓氏以及一个_id引用保存到Authors集合中的完整个人资料中.这样,我可以在单个查询中加载图书",仍然显示作者的姓名,然后生成指向作者个人资料的超链接:/author/{_id}.这就是所谓的数据非规范化",并且它使人胃灼热.我尝试将其用于不经常更改的数据(例如人们的名字).在名称确实发生更改的情况下,编写一个可以在多个位置更新所有名称的函数很简单.

For example, if I had a Book schema and Author schema, I would likely save the authors first and last name in the Books collection, along with an _id reference to the full profile in the Authors collection. That way I can load my Books in a single query, still display the author's name, and then generate a hyperlink to the author's profile: /author/{_id}. This is known as "data denormalization", and it has been known to give people heartburn. I try and use it on data that doesn't change very often - like people's names. In the occasion that a name does change, it's trivial to write a function to update all the names in multiple places.

这篇关于猫鼬:如何在不填充第一级字段的情况下填充第二级深层人口?在mongodb中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆