如何查询猫鼬中的两个集合?

时间:2018-08-02 13:42:19

标签: node.js mongodb mongoose

我有2个收藏集,分别是eventPart和Participant。

eventPart:{
   {
    "_id" : ObjectId("5b62676ba5419449046a26c4"),
    "rollNo" : "15K21A0528",
    "eventName" : "Ebullienza",
    "team" : 68,
     "__v" : 0
},{
    "_id" : ObjectId("5b62676ba5419449046a26c5"),
    "rollNo" : "15G11A0518",
    "eventName" : "Ebullienza",
    "team" : 68,
    "__v" : 0}
}

Participants : {
   {
     "_id" : ObjectId("5b62626d9306a610e36aca95"),
     "rollNo" : "15S11A0528",
     "name" : "Pavan Boro",
     "email" : "pavan@gmail.com",
     "mobile" : NumberLong(6700332950),
     "branch" : "CSE",
     "section" : "A",
     "year" : 4
}, {
     "_id" : ObjectId("5b62633d9306a610e36acae1"),
     "rollNo" : "15S11A0518",
     "name" : "Manoj",
     "email" : "manoj@gmail.com",
     "mobile" : NumberLong(9700332910),
     "branch" : "CSE",
     "section" : "A",
     "year" : 4
}}

我想用eventPart中的eventName以及Participant集合中的数据查询数据库。

例如:   与eventName:'Ebullienza'   我想在参与者集合中获取与eventName:'Ebullienza'相关的所有详细信息。

我尝试从eventPart收集rollNo并将其用于参与者集合中。 但是我被困在将rollNo收集到数组中

 var rollNumber = new Array();
 EventPart.find({'eventName':'Ebullineza'}).then(function(a){
    a.forEach(function(roll){
        rollNumber.push(roll.rollNo);
  });
});
console.log(rollNumber); // prints [] - empty array

rollNumber打印为[]。

我认为在猫鼬中可以有一种更有效的查询2个集合的方法。

谢谢。

2 个答案:

答案 0 :(得分:1)

使用$lookup

Dim intChoice As Integer
Dim strPath As String
'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add( _
    "CR Files Only", "*.cr")
'make the file dialog visible to the user
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made
If intChoice <> 0 Then
    'get the file path selected by the user
    crfile = Application.FileDialog( _
        msoFileDialogOpen).SelectedItems(1)

答案 1 :(得分:0)

在回调中打印您的数组...否则,将在执行查找结束之前打印

 var rollNumber = new Array();
 EventPart.find({'eventName':'Ebullineza'}).then(function(a){
    a.forEach(function(roll){
        rollNumber.push(roll.rollNo);
  });
  console.log(rollNumber); // prints [] - empty array
});