Hello I am using mongoose to search for similar posts in my collection.
/*Product model*/
const productSchema = mongoose.Schema(
{
writer: {
type: Schema.Types.ObjectId,
ref: "User",
},
title: {
type: String,
maxlength: 50,
},
description: {
type: String,
},
Category: {
type: String,
default: "Mobiles",
}
);
I can have access to only a single id at a time, what I want is to have access to all other posts
which have common string in their title, description or category.
In params I have only Product _id.
This is my code.
router.post("/MultiCards/:any", (req, res) => {
let order = req.body.order ? req.body.order : "desc";
let sortBy = req.body.sortBy ? req.body.sortBy : "_id";
let limit = req.body.limit ? parseInt(req.body.limit) : 100;
Product.find({ _id: req.params.any })
.sort([[sortBy, order]])
.limit(limit)
.exec((err, products) => {
if (err) return res.status(400).json({ success: false, err });
res
.status(200)
.json({ success: true, products, postSize: products.length });
});
});