2

以下のように関係を保持しているコレクションがいくつかあります.testMastertestDocの間の関係はtestDocMaster内に保持されています

例:

testMaster {
_id: "Schema.Objectid",
name: "String" //master name
}

testDoc {
_id : Schema.ObjectId,
name: "String", //doc name
other datas
}

testDocMaster {
masterId: "_id of the testMaster table",
docId : "_id of the above testDoc"
}

各マスターエントリには、多くのリレーションが予想されますが、

masterId がある場合、testDoc テーブルからデータを取得する最良の方法は何でしょうか。

4

2 に答える 2

1

私はこれを使ってそれを動かしました:

// GLOBAL ARRAYS FOR STORING COLLECTION DATA
var collectionOne = [];
var collectionTwo = [];
app.get('/', function(req, res){
  MongoClient.connect("mongodb://localhost:27017/michael", function(err, db) {
    if(!err) {
      console.log("We are connected");
    }
    db.collection("collectionOne", function(err, collection) {
      collection.find().sort({order_num: 1}).toArray(function(err, result) {
        if (err) {
          throw err;
        } else {
          for (i=0; i<result.length; i++) {
            collectionOne[i] = result[i];
          }
        }
      });
      db.collection("collectionTwo", function(err, collection) {
        collection.find().sort({order_num: 1}).toArray(function(err, result) {
          if (err) {
            throw err;
          } else {
            for (i=0; i<result.length; i++) {
              collectionTwo[i] = result[i];
            }
          }
        });
      });
      // Thank you aesede!
      res.render('index.html', {
        collectionOne: collectionOne,
        collectionTwo: collectionTwo
      });
    });
  });
});

ここで、なんらかの理由で Node を再起動して更新を押しても、フロントエンドに HTML がレンダリングされません。ただし、その後の更新では、ページが正しくレンダリングされます。

于 2013-10-08T00:26:20.670 に答える
0

testDocMasterスキーマが他の 2 つのコレクションのObjectId型を使用すると仮定すると、refMongoose のクエリ作成サポートを使用してこれを支援できます。

TestDocMaster.findOne({ masterId: masterId})
    .populate('docId')
    .exec(function(err, testDocMaster) {
        // testDocMaster.docId is populated with the full testDoc for the
        // matching _id
    });
于 2012-12-20T03:40:05.727 に答える