NodeJS 如何将一个系列适配到另一个系列?

yrdbyhpb  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(114)

我正在尝试将一个集合筛选到另一个集合文档,我得到了所有文档,而不是筛选文档

const getbookbycaterogry = async (req,res)=>{
    let query = req.query
   
    let filtercategory= await categoryModel.find({query:query}).select({_id:0})
    console.log("filtercategory",filtercategory)
   
    if(query.category){
        let categoryquery = req.query
        let getcategorydetail = await categoryModel.findOne({categoryquery:categoryquery})
        if(!getcategorydetail){
            res.send({status:false,message:"this category book not present here"})
        }
    }
    let bookdetail = await BookModel.find({})
    const obj = {
        filtercategory :filtercategory ,
        bookdetail :bookdetail
     }
     res.send({status:true,message:"book detail get sucessfully",obj})
}

我得到这个输出

{
  "obj": {
    "filtercategory": [
      {
        "category": "Biography",
        "ISBN": 8185301417,
        "createdAt": "2023-02-03T06:51:30.812Z",
        "updatedAt": "2023-02-03T06:51:30.812Z",
        "__v": 0
      },
      {
        "category": "novel",
        "ISBN": 8129135728,
        "createdAt": "2023-02-03T06:52:12.488Z",
        "updatedAt": "2023-02-03T06:52:12.488Z",
        "__v": 0
      },
      {
        "category": "novel",
        "ISBN": 8129135515,
        "createdAt": "2023-02-03T06:53:05.231Z",
        "updatedAt": "2023-02-03T06:53:05.231Z",
        "__v": 0
      }
    ],
    "bookdetail": [
      {
        "_id": "63dc99c66db9935e48a3a07f",
        "authorName": "chetan bhagat",
        "tittle": "half girlfriend",
        "category": "novel",
        "subcategory": [
          "love story of bihari boy"
        ],
        "Nopages": 280,
        "ISBN": 8129135728,
        "price": 400,
        "currency": "rs",
        "publisherBy": "Rupa & Co.",
        "publishedDate": "2023-02-04T18:30:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:21:10.084Z",
        "updatedAt": "2023-02-03T05:21:10.084Z",
        "__v": 0
      },
      {
        "_id": "63dc9ac46db9935e48a3a081",
        "authorName": "chetan bhagat",
        "tittle": "3 mistake of my life",
        "category": "novel",
        "subcategory": [
          "3 friend stroty"
        ],
        "Nopages": 280,
        "ISBN": 8129135515,
        "price": 300,
        "currency": "rs",
        "publisherBy": "aryan",
        "publishedDate": "2023-02-04T18:30:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:25:24.287Z",
        "updatedAt": "2023-02-03T05:25:24.287Z",
        "__v": 0
      },
      {
        "_id": "63dc9b596db9935e48a3a083",
        "authorName": "A. P. J. Abdul Kalam",
        "tittle": "Wings of Fire",
        "category": "novel",
        "subcategory": [
          "story about life of A. P. J. Abdul Kalam"
        ],
        "Nopages": 380,
        "ISBN": 9788173711466,
        "price": 350,
        "currency": "rs",
        "publisherBy": "Sangam Books Ltd.",
        "publishedDate": "2023-02-04T00:00:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:27:53.229Z",
        "updatedAt": "2023-02-03T05:27:53.229Z",
        "__v": 0
      },
      {
        "_id": "63dc9c466db9935e48a3a085",
        "authorName": "Ikigai",
        "tittle": "secrets to a happy life",
        "category": "motivation",
        "subcategory": [
          "story about how can we make life happy"
        ],
        "Nopages": 120,
        "ISBN": 764211242,
        "price": 1080,
        "currency": "rs",
        "publisherBy": "aryan Books Ltd.",
        "publishedDate": "2023-02-04T00:00:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:31:50.668Z",
        "updatedAt": "2023-02-03T05:31:50.668Z",
        "__v": 0
      },
      {
        "_id": "63dc9d256db9935e48a3a087",
        "authorName": "Swami Nikhilananda",
        "tittle": "Vivekananda:A Biography",
        "category": "Biography",
        "subcategory": [
          "story about life of Swami Nikhilananda"
        ],
        "Nopages": 320,
        "ISBN": 8185301417,
        "price": 980,
        "currency": "rs",
        "publisherBy": "Advaita Ashrama, India",
        "publishedDate": "2023-02-04T00:00:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:35:33.208Z",
        "updatedAt": "2023-02-03T05:35:33.208Z",
        "__v": 0
      }
    ]
  }
}

但我想要这样的输出:如果我搜索了小说,那么我会得到小说类别中的所有书籍

{
  "obj": {
    "filtercategory": [
      {
        "category": "novel"
      }
    ],
    "bookdetail": [
      {
        "_id": "63dc99c66db9935e48a3a07f",
        "authorName": "chetan bhagat",
        "tittle": "half girlfriend",
        "category": "novel",
        "subcategory": [
          "love story of bihari boy"
        ],
        "Nopages": 280,
        "ISBN": 8129135728,
        "price": 400,
        "currency": "rs",
        "publisherBy": "Rupa & Co.",
        "publishedDate": "2023-02-04T18:30:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:21:10.084Z",
        "updatedAt": "2023-02-03T05:21:10.084Z",
        "__v": 0
      },
      {
        "_id": "63dc9ac46db9935e48a3a081",
        "authorName": "chetan bhagat",
        "tittle": "3 mistake of my life",
        "category": "novel",
        "subcategory": [
          "3 friend stroty"
        ],
        "Nopages": 280,
        "ISBN": 8129135515,
        "price": 300,
        "currency": "rs",
        "publisherBy": "aryan",
        "publishedDate": "2023-02-04T18:30:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:25:24.287Z",
        "updatedAt": "2023-02-03T05:25:24.287Z",
        "__v": 0
      },
      {
        "_id": "63dc9b596db9935e48a3a083",
        "authorName": "A. P. J. Abdul Kalam",
        "tittle": "Wings of Fire",
        "category": "novel",
        "subcategory": [
          "story about life of A. P. J. Abdul Kalam"
        ],
        "Nopages": 380,
        "ISBN": 9788173711466,
        "price": 350,
        "currency": "rs",
        "publisherBy": "Sangam Books Ltd.",
        "publishedDate": "2023-02-04T00:00:00.000Z",
        "isDeleted": false,
        "createdAt": "2023-02-03T05:27:53.229Z",
        "updatedAt": "2023-02-03T05:27:53.229Z",
        "__v": 0
      }
    ]
  }
}

所以出来就像一个我的收藏书和另一个收藏类别,所以如果我是按类别过滤数据收集。

hmmo2u0o

hmmo2u0o1#

您只需要检查Book Model中的类别,如

const getbookbycaterogry = async (req, res) => {
  const { category } = req.query
  if (!category) {
    return res.status(400).send({ status: false, message: "Please add category" });
  }
  const categoryFilter = { category };
  const bookdetail = await BookModel.find(categoryFilter)
  if (bookdetail.length) {
    const obj = {
      filtercategory: categoryFilter,
      bookdetail: bookdetail
    }
    res.send({ status: true, message: "book detail get sucessfully", obj });
  } else {
    res.send({ status: false, message: "this category book not present here" });
  }
}

相关问题