node.js - Aggregate results from multiple Mongoose models -


there several models considerably different , belong different collections, yet have common fields used when query results aggregated together.

const blogpost = mongoose.model('blogpost', new mongoose.schema({   title: string,   body: string,   promoteatmainpage: {     type: boolean,     default: false   },   timestamp: date,   active: boolean,   / * rest different */ });  const article = mongoose.model('article', new mongoose.schema({   title: string,   body: string,   promoteatmainpage: {     type: boolean,     default: false   },   timestamp: date,   active: boolean,   / * rest different */ }); 

only common fields (title, body, timestamp) used result.

additionally, if promoteatmainpage missing in document, should treated differently in these models (it defaults true in 1 case , false in another).

currently done result processing:

let blogposts = await blogpost.aggregate([{ $match { active: true } }, { $sort: { timestamp: -1} }, { $limit: 100 } }]);  (let blogpost of blogposts)   blogpost.promoteatmainpage = ('promoteatmainpage' in blogpost)     ? blogpost.promoteatmainpage     : false;  let articles = await article.aggregate([{ $match { active: true } }, { $sort: { timestamp: -1} }, { $limit: 100 } }]);  (let article of articles)   article .promoteatmainpage = ('promoteatmainpage' in article )     ? article .promoteatmainpage     : true;  let mainpageposts = [...blogposts, ...articles]   .filter(post => post.promoteatmainpage)   .sort((a, b) => b.timestamp - a.timestamp)   .slice(0, 100); 

this results in requesting 200 documents instead of 100 , doing sort.

is possible in case aggregate results different collections means of mongoose or mongodb only?

can case missing promoteatmainpage field handled mongodb well, or reasonable way handle apply migration existing documents , add default promoteatmainpage value?


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -