Question

En utilisant Scala, MongoDB, Casbah.

Étant donné une liste aléatoire de chaînes:

  val names = {
    val listBuffer = new ListBuffer[String]
    for(n <- 1 to (new Random().nextInt(5) + 1)){
      val name = ((new Random().nextInt(26) + 65).asInstanceOf[Char]).toString
      listBuffer += name
    }
    listBuffer.toList
  }

Compte tenu d'une structure de document MongoDB:

"_id": <uuid>  
"name": <string>  

Comment trouver tous les documents qui ont un nom égal à une entrée dans ma liste en utilisant un seul Célibataire Instruction mongodbcollection.find ()? (c'est-à-dire en utilisant $ ou)

Merci, - Don

Était-ce utile?

La solution

MongoDB a un opérateur conditionnel $in Cela permet de tester si la valeur d'un champ est dans une liste de valeurs (Documentation)

collection.find({name: {$in: ["aaa", "bbb", "ccc"]}})

Dans Casbah, cela ressemblera

collection.find("name" $in names)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top