mongodb Mongoose查询无法从多个表中获取匹配数据

oknwwptz  于 2022-11-03  发布在  Go
关注(0)|答案(2)|浏览(178)

我正在使用nodejs,mongodb和mongoose。这个查询没有从用户和订单表中获取匹配数据。我读过mongoose文档和很多其他文章。它是根据它,但没有从表中获取匹配数据。

查询代码:

const data = await order
      .find({ admin_id: req.params.id })
      .populate("orderRelate");
      console.log(data);

我的用户和订单架构代码是:

const mongoose = require("mongoose");
const UserSchema = new mongoose.Schema(
  {
    username: { type: String, required: true, unique: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    mobile_no: { type: Number, required: true },
    profilePic: { type: String, defaut: "" },
    owner: { type: Boolean, defaut: false },
  },
  { timestamps: true }
);
const orderSchema = new mongoose.Schema(
  {
    customer_id: { type: String, required: true },
    admin_id: { type: String, required: true },
    order_type: { type: String, defaut: "normal" },
    order_status: { type: String, required: true },
    order_price: { type: Number, required: true },
    order_address: { type: String, required: true },
    order_pickDate: { type: String, required: true },
    order_pickTime: { type: String, required: true },
     orderRelate: [{ type: mongoose.Schema.Types.ObjectId, ref: "User" }],
  },
  { timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);
module.exports = mongoose.model("Order", orderSchema);

请让我知道,如果有人知道为什么我面临这个问题。谢谢

i7uaboj4

i7uaboj41#

请尝试使用findById,可能是工作

omtl5h9j

omtl5h9j2#

我不知道你是怎么写你的中间件函数的,但是..
等待订单。find({orderRelate:req.user.id)

相关问题