簡體   English   中英

如何保存與另一個外鍵關聯的文檔? 在 MONGODB 中(無 SQL)

[英]How to save a document that is associated to another foreign key? in MONGODB (NO SQL)

我想問一下如何通過獲取類別的 ID 來保存子類別。

類別 Model

  import mongoose from 'mongoose'

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

const Category = mongoose.model('Category', categorySchema)

export default Category

子類別 model

import mongoose from 'mongoose'

const subCategorySchema = mongoose.Schema({
    subname: {
    type: String,
    required: true,
},
categoryid: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'Category',
},
})

const SubCategory = mongoose.model('SubCategory', subCategorySchema)

export default SubCategory

我的 Controller

const createSubCategory = asyncHandler(async (req, res) => {
    const { subname, categoryid} = req.body

    const subCategoryExists = await SubCategory.findOne({
        subcategoryname,
    }).populate('categoryname')

    if (subCategoryExists) {
        res.status(400)
        throw new Error('SubCategory already exists')
    }

    const name = await Category.find({}).populate('_id')

    const subCategory = new SubCategory({
        categoryid: name,
        subcategoryname,
    })

    const createdSubCategory = await subCategory.save()

    if (createdSubCategory) {
        res.status(201).json({
            _id: createdSubCategory._id,
            categoryid: createdSubCategory,
            subname: createdSubCategory.subname,
        })
    } else {
        res.status(400)
        throw new Error('Invalid SubCategory')
    }
})

我的問題是

我將如何保存我的子類別,我將在其中 select 類別的 ID 值並將其插入到子類別 - 類別名稱字段中? 正在工作,但我得到的只是該類別中的第一個 ID,即使您輸入了另一個 ID。

我想在保存 mongodb 后擁有這樣的東西

子類別

{
    _id: 123,
    subcategory: "Sub Category",
    categoryname:{
        _id: 123456(categoryid),
        categoryname: "Categoryname"
    }
}

我已經設法弄清楚了,你需要讓它匹配。 在您的 request.body 中,您有一個名為 categoryid 的字段名稱,因此在 Category.FindOne({_id:} 中,您需要將您在 categoryid 中鍵入的內容與 _id 的 findone 相匹配,以便您得到它。:D

const createSubCategory = asyncHandler(async (req, res) => {
    const { subname, categoryid} = req.body

    const subCategoryExists = await SubCategory.findOne({
        subcategoryname,
    }).populate('categoryname')

    if (subCategoryExists) {
        res.status(400)
        throw new Error('SubCategory already exists')
    }

    const id= await Category.find({_id: categoryid}).populate('_id')

    const subCategory = new SubCategory({
        categoryid: id,
        subcategoryname,
    })

    const createdSubCategory = await subCategory.save()

    if (createdSubCategory) {
        res.status(201).json({
            _id: createdSubCategory._id,
            categoryid: createdSubCategory,
            subname: createdSubCategory.subname,
        })
    } else {
        res.status(400)
        throw new Error('Invalid SubCategory')
    }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM