猫鼬.stream没有填充字段

时间:2018-07-24 13:03:26

标签: javascript arrays mongodb mongoose populate

我有一个如下所示的模式“ Reports”:

    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)
  })

感谢帮助!

1 个答案:

答案 0 :(得分:0)

使用 <table id="addOidTable" class="table table-bordered"> <thead> <tr class="info"> <th style="width: 10px;"> <input type="checkbox" id="checkBox_add_b[]" onclick="selectAddAll(this)"/>SA</th> <th>name</th> <th>Oid </th> </tr> </thead> <tbody class="oid_table"> <% @all_b_oids.each do |oids| %> <tr id="tr_snmp_<%=oids['id'] %>"> <td> <input type="checkbox" class="checkBox" name="checkBox_Oid_B[]" onclick="clickedBoxAddAll(this.name)"> </td> <td style="word-break:break-all;"> <%= oids['name']%> </td> <td style="word-break:break-all;"> <%= oids['oid']%> </td> </tr> <% end %> </tbody> </table> 代替using System.IO; using System; using System.Collections.Generic; class Program { static void Main() { List<int> arr = new List<int>(){ 0, 1, 2, 1, 2, 3}; List<int> set = new List<int>(); int n = arr.Count; int max = 1; int i,j; for(i=0 ; i<n-1; i++) { set.Add(arr[i]); for(j=i+1; j<n; ) { if(Math.Abs(arr[i]-arr[j])<2) { if(!set.Contains(arr[j])) { if(set.Count == 2) break; else set.Add(arr[j]); } } else break; j++; } max = Math.Max(max,j-i); set.Clear(); } Console.WriteLine(max); } } ,它可以正常工作。