聚合中的 $lookup 在 mongo shell 命令中正常工作,但在尝试使用 mongoose 节点时,然后得到空数组作为响应

$lookup in aggregation working properly in mongo shell command, but while tried by using mongoose node, then getting empty array as response

$lookup in 聚合在 mongo shell 命令中正常工作,但在尝试使用 mongoose 节点时,然后得到空数组作为响应。 我有两个集合,并试图从一个集合中获取所有记录以及另一个集合的匹配记录作为响应对象的一部分

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}

博文评论

{"_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);

博文评论

    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()

控制器(在 mongoose 中,它不工作)

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: [] } ]

即使在使用 mongoose 时,传递给聚合操作的集合名称(例如 $lookup)也需要与 mongo 数据库中集合的确切名称相匹配- 不是指定给 mongoose 的模型名称。

因此,将查找更改为使用 blogpostcomments 而不是 Blogpostmessage 将解决此问题。

使用非 ID 字段作为键(无论是外键还是外键)也是不常见的,因此如果您的 localField 是“_id”并且 foreignField 是 postId 也会更好 - 您还应该添加一个如果您还没有这样做,请在 postId 上建立索引。

如果您想以 "mongoose" 方式执行此操作 - 请查看填充函数,其行为类似,但使用 mongoose 模型。

https://mongoosejs.com/docs/populate.html