$ lookup в агрегате работает должным образом в команде оболочки mongo, но при попытке использовать узел mongoose, а затем получить пустой массив в качестве ответа.
У меня есть две коллекции, и я попытался получить все записи из одной коллекции вместе с соответствующими записями другой коллекции как часть объектов ответа
мангуст v5.4.6
Образец данных блога
{"_id":"5d139addce3c200a1416f269","slug":"muthu-xperia","recent_post_title":"muthu","recent_post_desc":"xperia","recent_post_cont":"best","recent_post_trends":"true","meta_keywords":"null","recent_post_img":"http://localhost:3000/upload_resource/276992banner-4.jpg","active_status":"true","status":"true","createdAt":"2019-06-26T16:18:37.941Z","updatedAt":"2019-06-26T16:18:37.941Z","__v":0}
BlogPostComment
{"_id":"5d149d61127e78159c67eaee","post_id":"5d139addce3c200a1416f269","post_name":"muthu","post_message":"good afnoon","post_mail":"hiiii@gmail.com","status":"true","createdAt":"2019-06-27T10:41:37.832Z","updatedAt":"2019-06-27T10:41:37.832Z","__v":0}
схема BlogCol
const mongoose = require('mongoose');
const beautifyUnique = require('mongoose-beautiful-unique-validation');
const slugGen = require('mongoose-url-slugs');
// Admin- blog recent post model here
var blogSchema = new mongoose.Schema({
recent_post_title: {
type: String,
required: true
},
recent_post_desc: {
type: String
},
recent_post_cont: {
type: String
},
recent_post_trends: {
type: String
},
meta_keywords: {
type: String
},
recent_post_img: {
type: String
},
active_status: {
type: String
},
status: {
type: String
}
}, {
timestamps: true
});
blogSchema.plugin(beautifyUnique);
blogSchema.plugin(slugGen('recent_post_title recent_post_desc'));
mongoose.model('BlogCol', blogSchema);
BlogPostComment
const mongoose = require('mongoose');
const beautifyUnique = require('mongoose-beautiful-unique-validation');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var blogpostmessageSchema = new Schema({
post_id: {
type: ObjectId,
required:true
},
post_name: {
type: String,
},
post_message: {
type: String,
required: true
},
post_mail: {
type: String,
required: true
},
status: {
type: String
}
},{
timestamps: true
});
blogpostmessageSchema.plugin(beautifyUnique);
mongoose.model('BlogPostComment',blogpostmessageSchema)
Хорошо работает команда Mongo Shell
> db.blogcols.aggregate([{$lookup:{from:'blogpostcomments',localField:'_id',fore
ignField:'post_id',as:'comments'}}]).pretty()
Контроллер (в мангусте он не работает)
const mongoose = require('mongoose');
const Blog = mongoose.model('BlogCol');
const Blogpostmessage = mongoose.model('BlogPostComment');
Blog.aggregate([
{
$lookup: {
from: 'Blogpostmessage', // collection to join
localField: "recent_post_title",//field from the input documents
foreignField: "post_name",//field from the documents of the "from" collection
as: "postmessage"// output array field
}
}
],function(err,doc){
console.log(doc);
res.send(doc);
}
)
Фактический вывод
[ { _id: 5d139ac1ce3c200a1416f268,
slug: 'kumar-note5',
recent_post_title: 'kumar',
recent_post_desc: 'note5',
recent_post_cont: 'good',
recent_post_trends: 'true',
meta_keywords: 'null',
recent_post_img: 'http://localhost:3000/upload_resource/670051banner-5.jpg',
active_status: 'true',
status: 'true',
createdAt: 2019-06-26T16:18:09.419Z,
updatedAt: 2019-06-26T16:18:09.419Z,
__v: 0,
postmessage: [] },
{ _id: 5d139addce3c200a1416f269,
slug: 'muthu-xperia',
recent_post_title: 'muthu',
recent_post_desc: 'xperia',
recent_post_cont: 'best',
recent_post_trends: 'true',
meta_keywords: 'null',
recent_post_img: 'http://localhost:3000/upload_resource/276992banner-4.jpg',
active_status: 'true',
status: 'true',
createdAt: 2019-06-26T16:18:37.941Z,
updatedAt: 2019-06-26T16:18:37.941Z,
__v: 0,
postmessage: [] } ]
Ожидаемый результат
[ { _id: 5d139ac1ce3c200a1416f268,
slug: 'kumar-note5',
recent_post_title: 'kumar',
recent_post_desc: 'note5',
recent_post_cont: 'good',
recent_post_trends: 'true',
meta_keywords: 'null',
recent_post_img: 'http://localhost:3000/upload_resource/670051banner-5.jpg',
active_status: 'true',
status: 'true',
createdAt: 2019-06-26T16:18:09.419Z,
updatedAt: 2019-06-26T16:18:09.419Z,
__v: 0,
postmessage: [{
matched record of BlogPostComment model
}]
},
{ _id: 5d139addce3c200a1416f269,
slug: 'muthu-xperia',
recent_post_title: 'muthu',
recent_post_desc: 'xperia',
recent_post_cont: 'best',
recent_post_trends: 'true',
meta_keywords: 'null',
recent_post_img: 'http://localhost:3000/upload_resource/276992banner-4.jpg',
active_status: 'true',
status: 'true',
createdAt: 2019-06-26T16:18:37.941Z,
updatedAt: 2019-06-26T16:18:37.941Z,
__v: 0,
postmessage: [] } ]