2016-09-10 3 views
0

NodeJSを使用してクエリを実行しているMongoDBデータベースがあり、別の2つのコレクション( "appareil" & "bus")へのdbrefを含む2つのフィールドを持つコレクション "私は「MongoDBの」NPMモジュールを使用してNodeJsに次のコードを試してみましたNodeJSでfindを使用してmongodbを照会できません

{ 
     "_id" : ObjectId("..."), 
     "idService" : ..., 
     "codeRoute" : ..., 
     "codeCh" : "...", 
     "codeConv" : "...", 
     "codeLigne" : "...", 
     "date" : ISODate("..."), 
     "appareil" : { 
       "_id" : ObjectId("..."), 
       "_class" : "com.transtu.documents.Appareil", 
       "reference" : "...", 
       "societe" : DBRef("societe", ObjectId("...")) 
     }, 
     "bus" : { 
       "_id" : ObjectId("..."), 
       "_class" : "com.transtu.documents.Bus", 
       "code" : "...", 
       "immatriculation" : "...", 
       "reference" : "...", 
       "localisation" : { 
         "x" : ..., 
         "y" : ... 
       }, 
       "societe" : DBRef("societe", ObjectId("...")) 
     } 
} 

:ので、通常の位置ドキュメントは、これに似ているこのコードは動作していないと私はいつも「取得できない

findService = {}; 
findService.date = 'ISODate(' + new Date(service.date).toISOString() + ')'; 
findService.appareil = {_id: 'new ObjectID(' + service.appareil._id + ')'}; 
findService.bus = {_id: 'new ObjectID(' + service.bus._id + ')'}; 
findService.codeCh = service.codeCh; 
findService.codeConv = service.codeConv; 
serviceCollection.find(findService).toArray(function (err, docs) { 
             if (err) { 
              console.error('error:............... ' + JSON.stringify(err)); 
             } else { 

              //console.log('docs:............ ' + JSON.stringify(docs)); 
              if (docs.length == 0) { 
              console.log('not found'); serviceCollection.insertOne(service, function (res, err) { 
                if (err) { 
                 //console.error('err: ............: ' + JSON.stringify(err)); 
                } else { 
                 //console.log('res: **************: ' + JSON.stringify(res)); 
                } 
               }); 
              } else { 
               console.log('found'); 
               console.log('docs: ****************************** ' + JSON.stringify(docs)); 
              } 
             } 
            }); 

"答えを見つけましたが、mongoクライアントCMDを使用して、m E:この検索クエリの目標は、サービスのコレクション内の5つのフィールドの組み合わせの一意性を確保することである

db.service.find({"date": ISODate("..."), "codeCh": "...", "codeConv": "...", "appareil._id": ObjectId("..."), "bus._id" : ObjectId("...")}).pretty() 
が、私はこのような複合インデックスを追加しようとした

db.service.ensureIndex({date:1, appareil:1, bus:1, codeCh:1, codeConv:1}, { unique: true, dropDups: true }) 

はそれがなかったの

これらの5つのフィールドのみで検索クエリを処理するにはどうすればよいですか、またはこれらのフィールドの組み合わせをコレクション設定で一意にするにはどうすればよいですか?

答えて

0

コードが表示されている場合。 else状態にconsole.log('not found');があります。したがって、エラーがなければnot foundconsoleに印刷されます。私はあなたのコードが正常に動作していると思います。

serviceCollection.find(findService) 
    .toArray(function (err, docs) { 
     if (err) { 
      console.error('error:............... ' + JSON.stringify(err)); 
     } else { 
      console.log('not found');//this will print "not found" if there is no error. 
      //rest of the code 
     } 
    }); 

docsの長さを確認しながら、あなたはconsole.log('not found');を行う必要があります。

serviceCollection.find(findService) 
    .toArray(function (err, docs) { 
     if (err) { 
      console.error('error:............... ' + JSON.stringify(err)); 
     } else { 
      if (docs.length == 0) { 
       console.log('not found'); //this should be here 
       serviceCollection.insertOne(service, function (res, err) { 
       if (err) { 
        //console.error('err: ............: ' + JSON.stringify(err)); 
       } else { 
        //console.log('res: **************: ' + JSON.stringify(res)); 
       } 
       }); 
      } else { 
       console.log('found'); 
       console.log('docs: ****************************** ' + JSON.stringify(docs)); 
      } 
     } 
    }); 
+0

コンソールメッセージではなく、サービスコレクションにドキュメントが重複しています。とにかく自分のコードを編集しました –

関連する問題