如何解决Mongodb $ lookup检索菜单和子菜单作为子文档数组
看起来像一个老问题,但是我认为该模式稍有不同,我不确定这是否是正确的模式定义,但我想这样。
import { Schema,model } from 'mongoose';
import * as mongoose from 'mongoose';
const ObjectId = mongoose.Schema.Types.ObjectId;
const MenuSchema = new Schema({
parent: {
type: Schema.Types.ObjectId,ref: 'Menu',required: true,default: null },url: {
type: String,unique: true,},title: {
en: {
type: String,ar: {
type: String,type: {
// internal or external
type: String,required: false,default: 'internal',displayOrder: { type: Number,default: 0 },image: {
// For icon
type: Object,createdAt: { type: Date,default: Date.Now },createdBy: { type: ObjectId,ref: 'User' },updatedAt: { type: Date,updatedBy: { type: ObjectId,});
我的数据如下
{
"_id" : ObjectId("5f97be37bf401d75ed7de8a8"),"parent" : null,"displayOrder" : NumberInt(0),"image" : null,"title" : {
"en" : "Home","ar" : "الصفحة الرئيسية"
},"url" : "/"
}
{
"_id" : ObjectId("5f97eca04b076b2b8cb5646b"),"type" : "internal","url" : "/about","title" : {
"en" : "About","ar" : "حول"
},"createdAt" : ISODate("2020-10-27T09:47:12.396+0000"),"updatedAt" : ISODate("2020-10-27T09:47:12.396+0000"),"__v" : NumberInt(0),"displayOrder" : NumberInt(1),"parent" : null
}
{
"_id" : ObjectId("5f99492da0c7207dfb13aa03"),"url" : "/our-partners","title" : {
"en" : "Our Partners","ar" : "شركاؤنا"
},"createdAt" : ISODate("2020-10-28T10:34:21.353+0000"),"updatedAt" : ISODate("2020-10-28T10:34:21.354+0000"),"displayOrder" : NumberInt(8),"parent" : null
}
{
"_id" : ObjectId("5f9a7971f8443ecbdb787c4a"),"displayOrder" : NumberInt(2),"url" : "/services","title" : {
"en" : "Services","ar" : "خدمات"
},"createdAt" : ISODate("2020-10-29T08:12:33.366+0000"),"updatedAt" : ISODate("2020-10-29T08:12:33.366+0000"),"image" : null
}
{
"_id" : ObjectId("5f9a93f3dc3f16e672d9e84e"),"displayOrder" : NumberInt(3),"url" : "products","title" : {
"en" : "Products","ar" : "منتجات"
},"createdAt" : ISODate("2020-10-29T10:05:39.551+0000"),"updatedAt" : ISODate("2020-10-29T10:05:39.551+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b7a9c95213c7e3736e63e"),"displayOrder" : NumberInt(4),"url" : "projects","title" : {
"en" : "Projects","ar" : "المشاريع"
},"createdAt" : ISODate("2020-10-30T02:29:48.863+0000"),"updatedAt" : ISODate("2020-10-30T02:29:48.863+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b7aa395213c7e3736e63f"),"displayOrder" : NumberInt(5),"url" : "gallery","title" : {
"en" : "gallery","ar" : "صالة عرض"
},"createdAt" : ISODate("2020-10-30T02:29:55.845+0000"),"updatedAt" : ISODate("2020-10-30T02:29:55.845+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b7af995213c7e3736e640"),"displayOrder" : NumberInt(6),"url" : "downloads","title" : {
"en" : "Downloads","ar" : "التحميلات"
},"createdAt" : ISODate("2020-10-30T02:31:21.205+0000"),"updatedAt" : ISODate("2020-10-30T02:31:21.205+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b7b6095213c7e3736e641"),"displayOrder" : NumberInt(7),"url" : "contact","title" : {
"en" : "Contact","ar" : "اتصل"
},"createdAt" : ISODate("2020-10-30T02:33:04.071+0000"),"updatedAt" : ISODate("2020-10-30T02:33:04.071+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b8459391ab38846326a43"),"parent" : "5f97eca04b076b2b8cb5646b","url" : "about","title" : {
"en" : "Corporate Overview","ar" : "لمحة عن الشركة"
},"createdAt" : ISODate("2020-10-30T03:11:21.048+0000"),"updatedAt" : ISODate("2020-10-30T03:11:21.048+0000"),"image" : null
}
{
"_id" : ObjectId("5f9b8bb8391ab38846326a44"),"url" : "md-message","title" : {
"en" : "Md's Message","ar" : "رسالة د"
},"createdAt" : ISODate("2020-10-30T03:42:48.341+0000"),"updatedAt" : ISODate("2020-10-30T03:42:48.341+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b8c0e391ab38846326a45"),"url" : "certificates","title" : {
"en" : "Certificates","ar" : "الشهادات"
},"createdAt" : ISODate("2020-10-30T03:44:14.876+0000"),"updatedAt" : ISODate("2020-10-30T03:44:14.876+0000"),"image" : null
}
{
"_id" : ObjectId("5f9b8c4e391ab38846326a46"),"parent" : "5f9a7971f8443ecbdb787c4a","url" : "before-sale-service","title" : {
"en" : "Before Sale Service","ar" : "خدمة ما قبل البيع"
},"createdAt" : ISODate("2020-10-30T03:45:18.489+0000"),"updatedAt" : ISODate("2020-10-30T03:45:18.489+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b8c98391ab38846326a47"),"url" : "installation","title" : {
"en" : "Installation","ar" : "التركيب"
},"createdAt" : ISODate("2020-10-30T03:46:32.943+0000"),"updatedAt" : ISODate("2020-10-30T03:46:32.943+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b8ccb391ab38846326a48"),"url" : "maintenance","title" : {
"en" : "Maintenance","ar" : "اعمال صيانة"
},"createdAt" : ISODate("2020-10-30T03:47:23.943+0000"),"updatedAt" : ISODate("2020-10-30T03:47:23.944+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b8d04391ab38846326a49"),"url" : "repairs-modernization","title" : {
"en" : "REPAirs AND MODERNIZATION","ar" : "الإصلاح والتحديث"
},"createdAt" : ISODate("2020-10-30T03:48:20.958+0000"),"updatedAt" : ISODate("2020-10-30T03:48:20.958+0000"),"image" : null
}
{
"_id" : ObjectId("5f9b90180d34dd99a22729fc"),"parent" : "5f9b7aa395213c7e3736e63f","url" : "image-gallery","title" : {
"en" : "Image gallery","ar" : "معرض الصور"
},"createdAt" : ISODate("2020-10-30T04:01:28.652+0000"),"updatedAt" : ISODate("2020-10-30T04:01:28.652+0000"),"__v" : NumberInt(0)
}
{
"_id" : ObjectId("5f9b903a0d34dd99a22729fd"),"url" : "video-gallery","title" : {
"en" : "Video gallery","ar" : "معرض الفيديو"
},"createdAt" : ISODate("2020-10-30T04:02:02.999+0000"),"updatedAt" : ISODate("2020-10-30T04:02:02.999+0000"),"__v" : NumberInt(0)
}
我的用于检索带有子项(父项:someid)的父菜单(父项:null)的查询
db.getCollection("menus").aggregate([
{
$match: {
'parent': null
}
},{
$lookup: {
from: 'menus',localField: '_id',foreignField: 'parent',as: 'childs'
}
}
])
但是上述查询中我的孩子的子文档数组为空。
{
"_id" : ObjectId("5f97be37bf401d75ed7de8a8"),"url" : "/","childs" : [
]
}
{
"_id" : ObjectId("5f97eca04b076b2b8cb5646b"),"childs" : [
]
}
{
"_id" : ObjectId("5f99492da0c7207dfb13aa03"),"childs" : [
]
}
{
"_id" : ObjectId("5f9a7971f8443ecbdb787c4a"),"childs" : [
]
}
{
"_id" : ObjectId("5f9a93f3dc3f16e672d9e84e"),"childs" : [
]
}
我想通过将父列表和子菜单作为子文档来实现如下所示的结果。目前,我的子数组在上述查询中为空。看起来很简单,但是我很难找到。请帮忙。
[
{
"_id": "5f97be37bf401d75ed7de8a8","parent": null,"displayOrder": 0,"image": null,"title": {
"en": "Home","ar": "الصفحة الرئيسية"
},"url": "/","childs": []
},{
"_id": "5f97eca04b076b2b8cb5646b","type": "internal","url": "/about","title": {
"en": "About","ar": "حول"
},"createdAt": "2020-10-27T09:47:12.396Z","updatedAt": "2020-10-27T09:47:12.396Z","__v": 0,"displayOrder": 1,"childs" : [
{
"_id" : "5f9b8459391ab38846326a43","displayOrder" : 0,"title" : {
"en" : "Corporate Overview","ar" : "لمحة عن الشركة"
},"__v" : 0,"image" : null
},{
"_id" : "5f9b8c0e391ab38846326a45","displayOrder" : 1,"title" : {
"en" : "Certificates","ar" : "الشهادات"
},...
]
},]
解决方法
您在架构中的parent
字段被定义为type: Schema.Types.ObjectId,
,但是在数据库中,所有字段都被命名为string
。那是一种矛盾。
要解决您的问题,您只需要将parent
的所有string
字段类型替换为ObjectId
,因为$lookup
使用等号匹配,而string
使用ObjectId
不相等。
例如,您的一个文档应该看起来像这样:
{
"_id" : ObjectId("5f9b8bb8391ab38846326a44"),"parent" : ObjectId("5f97eca04b076b2b8cb5646b"),"type" : "internal","displayOrder" : NumberInt(1),"url" : "md-message","title" : {
"en" : "Md's Message","ar" : "رسالة د"
},"createdAt" : ISODate("2020-10-30T03:42:48.341+0000"),"updatedAt" : ISODate("2020-10-30T03:42:48.341+0000"),"__v" : NumberInt(0)
}
在本地进行了测试,可以确认其有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。