mongodb - MongoDB 聚合查找不适用于 localfield _id
问题描述
这是我的内容Schema
-
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ContentSchema = new Schema({
linkName: {
type: String,
},
noteUrl: {
type: String,
},
url: {
type: String,
},
description: {
type: String,
}
},
{
timestamps: true
});
module.exports = mongoose.model('Content', ContentSchema);
和内容评论Schema
——
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const contentCommentSchema = new Schema({
text: {
type: String,
},
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
content: {
type: Schema.Types.ObjectId,
ref: 'Content'
}
},
{
timestamps: true
});
module.exports = mongoose.model('contentComment', contentCommentSchema);
当我尝试$lookup
从 contentComment 中返回时,它每次都返回 0 条记录。但是在 db 中为相应的内容创建了注释。这是Aggregation
-
const contentPromise = Content.aggregate([
{
$lookup: {
from: "contentComments",
localField: "_id",
foreignField: "content",
as: "comments"
}
}]);
我在那里缺少什么吗?
解决方案
推荐阅读
- extjs - 如何在组合框中添加树
- java - 如何修复休眠异常 - 不要更改对启用了删除孤儿的集合的引用
- linux - 从每个数组中交替打印十个值,直到两者都为空
- python - 如何在 Pyspark 中将字符串更改为时间戳?
- variables - 如何处理源数据库中名称相同的字段,仅按标签名称区分?
- android - Android 哪个属性设置为绑定 onScrollChanged()
- c - 尽管已包含所有必需的文件,但如何修复“对‘某物’的未定义引用”?
- python - 从 CherryPy 运行 sudo 命令
- delphi - Delphi FMX - 如何阻止 TStringGrid 水平和垂直滚动
- r - 如何重现ggplot2的默认线型?