【发布时间】:2021-11-18 19:38:50
【问题描述】:
我试图将我的 Orders 架构中的所有数据与 Orders 架构中的接收者/发送者/driver.phoneNumber 字段之一匹配,我的 Orders 架构将这些字段作为对用户架构的引用,我的问题是当我搜索 phoneNumber 我希望它是正则表达式,这意味着它将返回一个文档数组,所以它不是一个值以便我在订单文档中查询它,我的代码是
else if (searchQuery) {
if (searchQuery.startsWith("07")) {
const num = "+964" + searchQuery.substring(1);
const users = await Register.find({ //this will return all users whom phoneNumber start with the query number
phoneNumber: { $regex: num },
});
orders = await Orders.find({
$or: [
{ receiverId: users }, //this query is obviously wrong, but im trying to implement something like this,
{ driverId: users },
{ senderId: users },
],
...branches,
})
.limit(limit)
.skip(skip)
.populate("receiverId")
.populate("driverId")
.populate("senderId")
如何返回与可能用户数组匹配的所有订单文档? 谢谢,
【问题讨论】:
-
你能显示从
Register和Orders集合返回的数据格式吗? -
订单:receiverId: { type: mongoose.Schema.Types.ObjectId, ref: "Register", }, driverId: { type: mongoose.Schema.Types.ObjectId, ref: "Register", }, senderId: { type: mongoose.Schema.Types.ObjectId, ref: "Register", },
-
fullName: { type: String, }, phoneNumber: { type: String, required: [true, "电话号码必填"], unique: [true, "这个电话号码以前用过"], },
-
顺便发布了相关字段,@TalESid
标签: node.js mongodb express mongoose