【问题标题】:Mongoose .stream doesn't have populated fieldsMongoose .stream 没有填充字段
【发布时间】:2019-01-01 02:51:33
【问题描述】:

我有一个看起来像这样的架构“报告”:

    var Reports = new Schema(
  {
    identifiersub: {            // Id of reported submission, populate stuff
      type: Schema.Types.ObjectId,
      ref: "Submission"
    },
    identifiercom: {            // Id of reported comment, populate stuff
      type: Schema.Types.ObjectId,
      ref: "Comments"
    },
    identifieruse: {            // Id of reported user, populate stuff
      type: Schema.Types.ObjectId,
      ref: "AccountDetails"
    },
    solved: {                   // Whether this problem has been solved or not
      type: Boolean,
      default: false
    },
    processed: [{               // Array of moderators who were participating in processing this report
      type: Schema.Types.ObjectId,
      ref: "AccountDetails"
    }],
    type: String,               // Type of content. bug, user, submission or comment
    reports: [                  // Additional text for each type of reported content
      {
        description: String,    // Text from select component. For bugs: the feature that is affected,
        reason: String,         // Reason for this report
        by: {                   // Reporter
          type: Schema.Types.ObjectId,
          ref: "AccountDetails"
        }
      }
    ],
    notes: [{                   // Admin notes
      title: String,            // Fleshed out discussion/reasoning
      note: String,             // Decided outcome that each note represents
      outcome: String,          // ("Keep reported", "delete", etc)
      date: Date,               // Date this note was added
      moderator: {              // Moderator who added this note
        type: Schema.Types.ObjectId,
        ref: "AccountDetails"
      }
    }]
  },
  { strict: false, timestamps: true }
)

在我的管理面板中,我想实现一个搜索功能,用指定的关键字搜索所有报告。这里最重要的字段是报告:它是一个对象数组,其中包含一个“by”字段,它是一个 ObjectId。现在我想为这个 id 填充用户名,但我没有在我的文档中看到它...我使用 .stream 方法检查整个文档,包括嵌套对象和对象数组。这是我的查询:

  var cursor = Reports
  .find({ type: req.query.type})
  .limit(500)
  .populate("notes.moderator reports.by processed", "username")
  .populate("identifieruse", "username dob email ipaddress")
  .populate("identifiercom", "by.username comment")
  .populate("identifiersub", "meta.title by deleted")
  .sort("-createdAt")
  .lean()
  .stream();

  cursor.on('data', function(doc) {
    console.log(doc);
    if (doc.toString().includes(key)) results.push(doc)
  })
  cursor.on('error', function(err) {
    return catcherror(new Error(err), res)
  })
  cursor.on('close', function() {
    console.log(results);
    return res.send(results)
  })

感谢您的帮助!

【问题讨论】:

  • 你可以在这里使用$lookup聚合...
  • 因此,如果我理解正确(根据文档),它会在它进入流/游标之前附加我使用 $lookup 选择的字段吗?
  • 是的,它将在管道中为您完成所有工作,并且不会使用游标来减少查询执行时间
  • 太棒了!非常感谢

标签: javascript arrays mongodb mongoose populate


【解决方案1】:

使用.cursor() 代替.stream() 即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 2017-05-21
    • 2013-07-06
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2015-09-19
    相关资源
    最近更新 更多