查询嵌套对象数组 [英] Querying nested object arrays

查看:98
本文介绍了查询嵌套对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别架构和一个示例数据,如下所示:

I have a category schema and an example data like below :

{
    categories: [], -- array of category object
    id: String,
    name: String
}

{ 
 "id" : 1,
 "name" : "cat1",
 "categories" : [ 
    {
    "id:2,
    "name": "cat2",
    "categories" : [
        {
        "id":3,
        "name": "cat3,
        }]
    }]
}



我想按类别名称获取类别,但只有说出

I want to get categories by their name, but I can only reach the first level if say something like

categories.findOne({"name": "cat1"}) -- returns cat1 correctly
categories.findOne({"name": "cat2"}) -- returns nothing

如何在子类别中搜索?

推荐答案

您可以使用 mongoose- mpath 包以创建树层次结构.

You can use mongoose-mpath package to create tree hierarchy.

以下是步骤:

1-)使用此插件创建类别架构.

1-) Create a category schema using this plugin.

const mongoose = require("mongoose");
const MpathPlugin = require("mongoose-mpath");

const schema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  }
});

schema.plugin(MpathPlugin);

const Category = mongoose.model("Category", schema);

module.exports = { Category };

2-)发布路线以插入具有层次结构的类别:

2-) post route to insert categories with hierarchy:

const express = require("express");
const router = express.Router();
const { Category } = require("../models/category");

router.post("/", async (req, res) => {
  const { name, parent } = req.body;

  const category = await Category.create({ name, parent });

  res.status(201).send(category);
});

使用此路线,使用邮递员创建类别:

Using this route, create the categories with postman:

根目录类别:(最好有一个根目录类别,根目录没有父目录)

Root Category: (it is good to have a ROOT category, root has no parent)

{
    "name": "ROOT"
}

响应如下:(如您所见,此插件在内部添加了一个路径字段以维护层次结构)

The response will be like this: (as you see this plugin added a path field internally to maintain hierarchy)

{
    "_id": "5dd95adf997fc53e1ce944a7",
    "name": "ROOT",
    "path": "5dd95adf997fc53e1ce944a7",
    "__v": 0
}

Cat1类别:(请注意,我们将父字段设置为根类别ID,即5dd95adf997fc53e1ce944a7)

Cat1 category: (note that we set parent field to the root category id which is 5dd95adf997fc53e1ce944a7)

{
    "name": "Cat1",
    "parent": "5dd95adf997fc53e1ce944a7"
}

Cat1的结果:

{
    "_id": "5dd95b21997fc53e1ce944a8",
    "name": "Cat1",
    "parent": "5dd95adf997fc53e1ce944a7",
    "path": "5dd95adf997fc53e1ce944a7#5dd95b21997fc53e1ce944a8",
    "__v": 0
}

Cat11类别:(请注意,我们将父字段设置为Cat1 id,即5dd95adf997fc53e1ce944a7)

Cat11 category: (note that we set parent field to the Cat1 id which is 5dd95adf997fc53e1ce944a7)

{
    "name": "Cat11",
    "parent": "5dd95adf997fc53e1ce944a7"
}

Cat11的结果:

{
    "_id": "5dd95b8e997fc53e1ce944aa",
    "name": "Cat11",
    "parent": "5dd95adf997fc53e1ce944a7",
    "path": "5dd95adf997fc53e1ce944a7#5dd95b8e997fc53e1ce944aa",
    "__v": 0
}

要按名称查找类别,可以使用以下获取路线: (为简单起见,我对类别名称进行了硬编码,您可以从req.body或req.query中获取类别名称)

And to find a category by name, you can use the following get route: (I hard coded category name for simplicity, you cantake category name from req.body or req.query)

router.get("/", async (req, res) => {
  let category = await Category.findOne({
    name: "Cat11" 
  });

  res.status(200).send(category);
});

这将为您返回Cat11类别文档,如下所示:

This will return you the Cat11 category document like this:

{
    "_id": "5dd95b8e997fc53e1ce944aa",
    "name": "Cat11",
    "parent": "5dd95adf997fc53e1ce944a7",
    "path": "5dd95adf997fc53e1ce944a7#5dd95b8e997fc53e1ce944aa",
    "__v": 0
}

如果要获取找到的类别的子类别,可以尝试使用mongoose-mpath方法,例如getChildrenTree.

And if you want to get the children categories of the found category, you can try mongoose-mpath methods like getChildrenTree.

这篇关于查询嵌套对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆