Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Nodejs聚合查找返回空数组_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript Nodejs聚合查找返回空数组

Javascript Nodejs聚合查找返回空数组,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我有一个用户模型 const mongoose = require("mongoose"); const uniqueValidator = require("mongoose-unique-validator"); const userSchema = mongoose.Schema({ email: { type: String, required: true, unique: true }, password: { type: String

我有一个用户模型

const mongoose = require("mongoose");
const uniqueValidator = require("mongoose-unique-validator");

const userSchema = mongoose.Schema({
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  isEmailVerified: { type: Boolean },
  registrationStep: { type: Number, enum: [0,1,2,3]},
  regDate: { type: Date },
  companyName: { type: String },
  oib: { type: String },
  telephone: { type: String },
  address: { type: String },
  city: { type: String },
  countryCode: { type: String },
  postalCode: { type: String },
  userType: { type: String, enum:['firms','drivers','both']},
  approved: { type: Boolean },
  isAdmin: { type: Boolean }
});

userSchema.plugin(uniqueValidator);

module.exports = mongoose.model("User", userSchema);
和文档模型

const mongoose = require("mongoose");

const docsSchema = mongoose.Schema({
  docsPath: { type: String, required: true },
  creator: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
  docType: { type: String, enum:['isr','lmp','pocmr'], required: true }
});

module.exports = mongoose.model("Docs", docsSchema);
我希望加入集合,以便每个用户都存在user_docs字段,其中包含文档数据,如下面的代码所示。我正试图通过在创建文档时定义的将_id与creator连接起来来实现这一点

exports.getUsersAndDocs = (req, res, next) => {
  const query = User.aggregate([
    {
    $lookup: {
      from: "Docs",
      localField: "_id",
      foreignField: "creator",
      as: "user_docs"
    }
  }]);
  query
    .then(fetchedUsers => {
      res.status(200).json({
        message: "Users fetched successfully!",
        users: fetchedUsers,
      });
    })
    .catch(error => {
      res.status(500).json({
        message: "Fetching users failed!"
      });
    });
};

我收到的是空的用户文档数组。如果有人有同样的问题,感谢您的帮助。我通过改变来解决它

from: "Docs"

from: Docs.collection.name