[英]Mongoose find documents based on subdocument reference value
我正在尝试根据子文档参考获取文档。
我试过了
Blog.find({"user.blocked" : false})
该查询不会提取任何文档结果。
这是我的架构
const BlogSchema = new mongoose.Schema({
category: {type: String, trim: true},
user: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
title: {type: String},
description: {type: String},
}, {timestamps: true});
const UserSchema = new mongoose.Schema({
name: {type: String, required: true},
blocked: Boolean,
}, {timestamps: true});
mongoose.model('User', UserSchema);
mongoose.model('Blog', BlogSchema);
您将用户定义为参考,因此您无法查询其他集合的字段。 这是与关系数据库的区别之一,您不能执行JOIN
。
您的替代方法是使用聚合而不是查询,使用$lookup
运算符。 你可以在这里查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.