我知道如何使用MongoDB Atlas搜索来搜索按其名称和给定买家ID以及已知/matched
供应商名称的订单,例如:
(使用猫鼬ODM)
const searchResults = await Order.aggregate([
{
$search: {
text: {
query: '{search-term}',
path: 'name',
fuzzy: {
maxEdits: 2,
maxExpansions: 100,
},
},
},
},
{ $unwind: '$supplier' },
{
$lookup: {
from: 'suppliers',
localField: 'supplier',
foreignField: '_id',
as: 'suppliers',
},
},
{
$match: {
buyer: mongoose.Types.ObjectId('5e19a594c86e72017debf9dc'),
// The search term entered by a user:
'suppliers.name': 'supplierName',
},
},
]);
但是,我希望能够利用MongoDB Atlas搜索来:
Buyer ID
,Supplier name
,具有Order
架构:
const orderSchema = new mongoose.Schema({
name: {
type: String,
minlength: 2,
maxlength: 255,
},
buyer: { type: ObjectId, ref: 'Buyer' },
supplier: { type: ObjectId, ref: 'Supplier' },
});
Buyer
模式:
const buyerSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
Supplier
模式:
const supplierSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句