MongoDB中两个不同集合之间的键值交叉

时间:2016-12-04 13:59:34

标签: node.js mongodb

我有2个集合:public class PublicFolder { public string DisplayName { get; set; } public FolderId FolderID { get; set; } public int Childs { get; set; } } private void DoMagic() { try { List<PublicFolder> RootFolders = new List<PublicFolder>(); ExchangeService ES = new ExchangeService(ExchangeVersion.Exchange2010_SP2); try { ES.Credentials = new WebCredentials("email@domain.com", "password"); ES.AutodiscoverUrl("email@domain.com"); Folder F = Folder.Bind(ES, WellKnownFolderName.PublicFoldersRoot); F.Load(); foreach (Folder fol in F.FindFolders(new FolderView(F.ChildFolderCount))) { PublicFolder PF = new PublicFolder(); PF.DisplayName = fol.DisplayName; PF.Childs = fol.ChildFolderCount; PF.FolderID = fol.Id; RootFolders.Add(PF); } foreach (PublicFolder PF in RootFolders) { Folder Q = Folder.Bind(ES, PF.FolderID); { if (Q.ChildFolderCount > 0) { Q.Load(); if (Q.DisplayName == "Special Department's Folder") { foreach (Folder W in Q.FindFolders(new FolderView(Q.ChildFolderCount))) { if (W.DisplayName == "Inbox Folder") { Folder R = Folder.Bind(ES, W.Id); { foreach (Item I in R.FindItems(new ItemView(R.TotalCount))) { //THIS IS WHERE I WANT TO GET ITEM'S CUSTOM FIELDS FROM CUSTOM FORM } } } } } } } } } catch (Exception E) { MessageBox.Show(E.Message); } } } bookings

timeslots

models/booking.js:

var mongoose = require ('../config/db'); var Schema = require('mongoose').Schema; var ObjectId = Schema.ObjectId; var bookingSchema = new Schema({ start: { type: Number, required: true }, end: { type: Number, required: true }, date: { type: Date, required: true, default: Date.now } });

models/time_slot.js:

两者都有一个共同的字段var mongoose = require ('../config/db'); var Schema = require('mongoose').Schema; var ObjectId = Schema.ObjectId; var timeSlotSchema = new Schema({ start: { type: Number, required: true }, end: { type: Number, required: true }, date: { type: Number, required: true, default: Time.Today }, enabled: { type: Boolean, required: true, default: true, }, pickup: { type: Boolean, required: true, default: true, } }); 。我希望能够从starttimeslots的{​​{1}}的{​​{1}}集合中获取条目。

我试过了:

start

bookings

但是,这并不令人惊讶,并且我得到了错误:

controllers/time_slot.js:

1 个答案:

答案 0 :(得分:0)

你可以这样做:

Booking.getBookings({}, function(err, bookings) {
  if (err) {
    console.error(err);
  } else {

    // build array with unique "start" values from bookings
    var starts = bookings
      .map(booking => booking.start)
      .filter((val, i, self) => self.indexOf(val) === i)

    // find by these start values
    var query = {start: {$in: starts}}

    timeSlotModel.find(query, function(err, slots) {
      if (err) {
        console.error(err);
      } else {
        return next(null, slots);
      }
    }
}