2017-01-31 25 views
0

2つのコレクションがあります 1)Floorplan_backup。 文書の構造は次のようになります。 -Mongodbコレクションのドキュメントを別のコレクションのフィールド値で更新する方法

{ 
    "_id" : ObjectId("5877e18a88db272b578572bd"), 
    "floorplans" : [ 
     { 
      "name" : "Campus Center - Atrium", 
      "uuid" : NumberLong(3), 
      "cameras" : [] 
     }, 
     { 
      "name" : "Campus Center - Bridge ", 
      "uuid" : NumberLong(4), 
      "cameras" : [] 
     }, 
     { 
      "name" : "Campus Center - Top", 
      "uuid" : NumberLong(5), 
      "cameras" : [] 
     }, 
     { 
      "name" : "Performance Dining Hall", 
      "uuid" : NumberLong(6), 
      "cameras" : [] 
     }, 
     { 
      "name" : "Main Kitchen", 
      "uuid" : NumberLong(7), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Board of Trustees", 
      "uuid" : NumberLong(8), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Academic Affairs", 
      "uuid" : NumberLong(9), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Admissions", 
      "uuid" : NumberLong(10), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Athletics", 
      "uuid" : NumberLong(11), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Financial", 
      "uuid" : NumberLong(14), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - HR", 
      "uuid" : NumberLong(15), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Institutional", 
      "uuid" : NumberLong(16), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - IT", 
      "uuid" : NumberLong(17), 
      "cameras" : [] 
     }, 

     { 
      "name" : "OrgChart - Student Success", 
      "uuid" : NumberLong(20), 
      "cameras" : [] 
     }, 
     { 
      "name" : "OrgChart - Student Life", 
      "uuid" : NumberLong(21), 
      "cameras" : [] 
     } 
    ], 
    "userid" : "user-56cb3c4c0c953470865336", 
    "firstviewhost" : undefined 
} 

2)フロアプラン。文書

{ 
    "_id" : ObjectId("589025b03422cafc09913363"), 
    "userid" : "user-56cb3c4c0c953470865336", 
    "firstviewhost" : "dean.vizsafe.com" 
} 

の構造は、私は両方のコレクションのユーザーIDを照合することによって値「dean.vizsafe.com」とFloorpla_backupでfirstviewhostを更新したいと思います。

db.floorplan_backup.find().forEach(function (doc1) { 
    var doc2 = db.floorplan.find({ userid: doc1.userid }, { firstviewhost: 1 }); 
    if (doc2 != null) { 
     doc1.firstviewhost = doc2.firstviewhost; 
     db.floorplan_backup.save(doc1); 
    } 
}); 

しかし、私に希望を与えて結果セットされていないです - :

使用するJavaScriptコードがあります。これに間違っていることを教えてください。

答えて

0

findは、あなたにカーソルを返し、代わりにfindOneを使用します。

db.floorplan_backup.find().forEach(function (doc1) { 
    var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 }); 
    if (doc2 != null) { 
     doc1.firstviewhost = doc2.firstviewhost; 
     db.floorplan_backup.save(doc1); 
    } 
}); 
+0

ありがとう、それは私のために働いた – Webdev

1

doc2は配列です。 findOneを使用してオブジェクトを取得します。

db.floorplan_backup.find().forEach(function (doc1) { 
    var doc2 = db.floorplan.findOne({ userid: doc1.userid }, { firstviewhost: 1 }); 
    if (doc2 != null) { 
     doc1.firstviewhost = doc2.firstviewhost; 
     db.floorplan_backup.save(doc1); 
    } 
}); 
関連する問題