node.js - Mongoose: How to populate 2 level deep population without populating fields of first level? in mongodb -


here mongoose schema:

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,  .......  .......  ....... }); 

while access schemaa using find query, want have fields/property of schemaa along schemab , schemac in same way apply join operation in sql database.

this approach:

schemaa.find({})  .populate('fieldb')  .exec(function (err, result){          schemab.populate(result.fieldc,{path:'fieldb'},function(err, result){      .............................         });  });  

the above code working perfectly, problem is:

  1. i want have information/properties/fields of schemac through schemaa, , don't want populate fields/properties of schemab.
  2. the reason not wanting properties of schemab is, population slows query unnecessary.

long story short: want populate schemac through schemaa without populating schemab.

can please suggest way/approach?

as avid mongodb fan, suggest use relational database highly relational data - that's it's built for. losing benefits of mongodb when have perform 3+ queries single object.

buuuuuut, know comment fall on deaf ears. best bet conscious can performance. first step limit fields minimum required. practice basic queries , any database engine - only fields need (eg. select * from === bad... stop doing it!). can try doing lean queries save lot of post-processing work mongoose data. didn't test this, should work...

schemaa.find({}, 'field1 fieldb', { lean: true }) .populate({     name: 'fieldb',     select: 'fieldc',     options: { lean: true } }).exec(function (err, result) {     // not sure how populating "result" in example, should array,      // said code works... i'll let figure out goes here. }); 

also, "mongo" way of doing want save reference in schemac schemaa. when "mongo" way of doing it, have break away years of thinking relational data queries. whatever takes perform fewer queries on database, if requires two-way references and/or data duplication. example, if had book schema , author schema, save authors first , last name in books collection, along _id reference full profile in authors collection. way can load books in single query, still display author's name, , generate hyperlink author's profile: /author/{_id}.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -