MongoDB - 如何从所有 collections 中找到所有其他相关文档。我只能访问 1 个 ID
MongoDB - How can I find all other related documents from all collections. I have access to only 1 id
您好,我正在使用 mongoose 在我的 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",
}
);
我一次只能访问一个id,我想要访问所有其他帖子
有共同点
标题、描述或类别中的字符串。
在参数中我只有 Product _id。
这是我的代码。
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 });
});
});
如果您真的只能访问产品的 _id,则找到该产品,然后使用返回的对象通过 find
搜索类似产品
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.findById(req.params.any)
.sort([[sortBy, order]])
.limit(limit)
.exec((err, product) => {
if (err) return res.status(400).json({ success: false, err });
Product.find({ Category: product.Category}).then((products) => {
res
.status(200)
.json({ success: true, products, postSize: products.length });
})
});
});
ES2016版本
router.post('/MultiCards/:any', async (req, res) => {
const order = req.body.order ? req.body.order : 'desc';
const sortBy = req.body.sortBy ? req.body.sortBy : '_id';
const limit = req.body.limit ? parseInt(req.body.limit) : 100;
try {
const product = await Product.findById(req.params.any).sort([[sortBy, order]])
.limit(limit).exec();
const products = await Product.find({ title: `/${product.title}/i` });
res
.status(200)
.json({ success: true, products, postSize: products.length });
} catch (error) {
res.status(400).json({ success: false, error });
}
如果你想要类似的查询,请像这样查询 /query/i
如猫鼬文档中所写
// executes, name LIKE john and only selecting the "name" and "friends" fields
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })
您好,我正在使用 mongoose 在我的 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",
}
);
我一次只能访问一个id,我想要访问所有其他帖子
有共同点 标题、描述或类别中的字符串。
在参数中我只有 Product _id。
这是我的代码。
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 });
});
});
如果您真的只能访问产品的 _id,则找到该产品,然后使用返回的对象通过 find
搜索类似产品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.findById(req.params.any)
.sort([[sortBy, order]])
.limit(limit)
.exec((err, product) => {
if (err) return res.status(400).json({ success: false, err });
Product.find({ Category: product.Category}).then((products) => {
res
.status(200)
.json({ success: true, products, postSize: products.length });
})
});
});
ES2016版本
router.post('/MultiCards/:any', async (req, res) => {
const order = req.body.order ? req.body.order : 'desc';
const sortBy = req.body.sortBy ? req.body.sortBy : '_id';
const limit = req.body.limit ? parseInt(req.body.limit) : 100;
try {
const product = await Product.findById(req.params.any).sort([[sortBy, order]])
.limit(limit).exec();
const products = await Product.find({ title: `/${product.title}/i` });
res
.status(200)
.json({ success: true, products, postSize: products.length });
} catch (error) {
res.status(400).json({ success: false, error });
}
如果你想要类似的查询,请像这样查询 /query/i
如猫鼬文档中所写
// executes, name LIKE john and only selecting the "name" and "friends" fields
MyModel.find({ name: /john/i }, 'name friends', function (err, docs) { })