Mongoose MongoDB Atlas按参考文档字段搜索词搜索
我知道如何通过名称和给定的买家ID以及已知的/Mongoose MongoDB Atlas按参考文档字段搜索词搜索,mongoose,mongodb-atlas,mongodb-atlas-search,Mongoose,Mongodb Atlas,Mongodb Atlas Search,我知道如何通过名称和给定的买家ID以及已知的/匹配的供应商名称来搜索订单 例如: (使用猫鼬ODM) 但是,我希望能够利用MongoDB Atlas搜索: 搜索所有订单 给定一个买方ID 其中搜索词-可能不是完全匹配-是供应商名称 如果买方和供应商通过引用ID嵌套在订单上: 具有顺序模式: const orderSchema = new mongoose.Schema({ name: { type: String, minlength: 2, maxlength:
匹配的
供应商名称来搜索订单
例如:
(使用猫鼬ODM)
但是,我希望能够利用MongoDB Atlas搜索:
- 搜索所有订单
- 给定一个
买方ID
- 其中搜索词-可能不是完全匹配-是供应商名称
- 如果买方和供应商通过引用ID嵌套在订单上:
顺序
模式:
const orderSchema = new mongoose.Schema({
name: {
type: String,
minlength: 2,
maxlength: 255,
},
buyer: { type: ObjectId, ref: 'Buyer' },
supplier: { type: ObjectId, ref: 'Supplier' },
});
const buyerSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
const supplierSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
买方
模式:
const orderSchema = new mongoose.Schema({
name: {
type: String,
minlength: 2,
maxlength: 255,
},
buyer: { type: ObjectId, ref: 'Buyer' },
supplier: { type: ObjectId, ref: 'Supplier' },
});
const buyerSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
const supplierSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
供应商
模式:
const orderSchema = new mongoose.Schema({
name: {
type: String,
minlength: 2,
maxlength: 255,
},
buyer: { type: ObjectId, ref: 'Buyer' },
supplier: { type: ObjectId, ref: 'Supplier' },
});
const buyerSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
const supplierSchema = new mongoose.Schema({
name: {
type: String,
},
...
});
我已经通过以下内容实现了所需的结果,但它没有使用Atlas搜索,因此我希望您能提供更多帮助
const order = await Order.aggregate([
{ $unwind: '$supplier' },
{
$lookup: {
from: 'suppliers',
localField: 'supplier',
foreignField: '_id',
as: 'suppliers',
},
},
{
$match: {
buyer: mongoose.Types.ObjectId('5e19a594c86e72017debf9dc'),
'suppliers.name': new RegExp('^' + supplierName, 'i'),
},
},
]);
这应该可以使用,并且在$search
中:
const order = await Order.aggregate([
{ $search: {
compound: {
must: [
{ text: { query: "SEARCH TERM", path: "name"}},
{ equals: { path: "buyer", value: ObjectId("5e19a594c86e72017debf9dc") }}
]
}
}}])
如果不想影响评分,也可以将equals查询移动到filter子句