2017-03-25 3 views
0

私はこれを行いましたが、作成者フィールドに2つの特定の値と他の値が含まれているすべての文書を返します。配列フィールドに2つの特定の値しか含まれていないすべての文書を見つける方法

Document authors = new Document("authors","firstValue") 
    .append("authors", "secondValue"); 

    MongoCursor<Document> cursor = collection.find(authors).iterator(); 

    try { 
     while (cursor.hasNext()) { 
      Document doc = cursor.next(); 
      System.out.println(doc.get("title")+ " " + doc.get("authors")); 
     } 
    } 
    finally { 
     cursor.close(); 
    } 
    client.close(); 

これらの2つの値のみを含むドキュメントのみを検索したいとします。

答えて

1

secondValueauthorsキーに追加して値を上書きしています。フィルタは{ "authors" : "secondValue" }のように見えます。

ですから、あなたが{ "authors" : [ "firstValue", secondValue" ] }

Document authors = new Document("authors", Arrays.asList("firstValue", "secondValue"))のようなフィルタを必要とするためなど、2つだけの値を含む配列を持つ文書が必要な場合は、

関連する問題