查找数组元素与reactivemongo中某些条件匹配的所有文档

时间:2014-10-15 09:02:47

标签: mongodb scala reactivemongo nosql

在我的MongoDB数据库中,我有一个名为" recommended.users"其中我存储了所有用户数据。 我的收藏品的文件格式如下:

{
    "_id" : ObjectId("542e67e07f724fc2af28ba75"),
    "id" : "",
    "email" : "luigi@gmail.com",
    "tags" : [
        {
            "tag" : "Paper Goods:Liners - Baking Cups",
            "weight" : 2,
            "lastInsert" : 1412327492874
        },
        {
            "tag" : "Vegetable:Carrots - Jumbo",
            "weight" : 4,
            "lastInsert" : 1412597883569
        },
        {
            "tag" : "Paper Goods:Lialberto- Baking Cups",
            "weight" : 1,
            "lastInsert" : 1412327548205
        },
        {
            "tag" : "Fish:Swordfish Loin Portions",
            "weight" : 3,
            "lastInsert" : 1412597939124
        },
        {
            "tag" : "Vegetable:Carrots - alberto@gmail.com",
            "weight" : 2,
            "lastInsert" : 1412597939124
        }
    ]
}

作为驱动程序,我在Scala中使用ReactiveMongo。

现在我正在编写一个获取tags: List[String]的方法,并在我的集合中搜索"tags"元素包含作为参数传递的所有值的所有用户。 换句话说,我想找到所有文件中的"标签"标记包含"tag" = tags(i)的每个元素tags的文档。 我如何在MongoDB和reactiveMongo中创建这样的东西?

我的方法有以下签名:

    def findUsers(tags: List[String]): Future[Option[List[User]]] = {
      //search all the matching Users
      //if exists at least one User return a Some(List(users))
      //otherwise returns a None

}

例如,如果我的收藏中有以下两个文件:

[
{
    "_id" : ObjectId("542e67e07f724fc2af28ba75"),
    "id" : "",
    "email" : "luigi@gmail.com",
    "tags" : [
        {
            "tag" : "Paper Goods:Liners - Baking Cups",
            "weight" : 2,
            "lastInsert" : 1412327492874
        },
        {
            "tag" : "Vegetable:Carrots - Jumbo",
            "weight" : 4,
            "lastInsert" : 1412597883569
        },
        {
            "tag" : "Paper Goods:Lialberto- Baking Cups",
            "weight" : 1,
            "lastInsert" : 1412327548205
        },
        {
            "tag" : "Fish:Swordfish Loin Portions",
            "weight" : 3,
            "lastInsert" : 1412597939124
        },
        {
            "tag" : "Vegetable:Carrots - alberto@gmail.com",
            "weight" : 2,
            "lastInsert" : 1412597939124
        }
    ]
},

{
    "_id" : ObjectId("542e67e07f724fc2af28ba75"),
    "id" : "",
    "email" : "alberto@gmail.com",
    "tags" : [
        {
            "tag" : "Paper Goods:Lialberto- Baking Cups",
            "weight" : 1,
            "lastInsert" : 1412327548205
        },
        {
            "tag" : "Fish:Swordfish Loin Portions",
            "weight" : 3,
            "lastInsert" : 1412597939124
        },
        {
            "tag" : "Vegetable:Carrots - alberto@gmail.com",
            "weight" : 2,
            "lastInsert" : 1412597939124
        }
    ]
}]

如果使用findUser调用我的方法(List(" Fish:Swordfish Loin Portions"," Vegetable:Carrots - alberto@gmail.com"))我的方法必须返回第一位用户。

我知道可以通过一个循环来检查用户是否拥有所有给定的标签,但它过于复杂和冗长。存在另一种选择??

我该怎么做?

1 个答案:

答案 0 :(得分:1)

使用

之类的查询
db.test.find({$and:[{"tags.tag":"a"},{"tags.tag":"b"}]})

您可以使用此reactivemongo-query https://github.com/sh1ng/ReactiveMongo-Queries

来简化您的生活
val cursor = collection.find(on[Interests].and(_.eq(_.tags.tag, "Fish:Swordfish Loin Portions"), _.eq(_.tags.tag, "Vegetable:Carrots - alberto@gmail.com"))).cursor[Interests]

当然你可以用pure reactivemongo重写它。