I've a MongoDB collection that store all the user data.
A document of my collection has the following JSON form:
{
"_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
}
]
}
The tag
field is in the form "category:name product"
and the "tags"
field contains all the product bought by an user.
I'm writing a Scala application, and I'm using the reactivemongo driver.
Now I'm writing a method that given a category
and a product
search all the user that have bought at least a product of the given category, but had not already bought no one product equals to the given.
My code now is like the following:
def findUsers(input: FindSuggestion): Future[Option[List[User]]] = {
val category = input.category //a string
val product = input.product //a string, in the form category:productName
val query = Json.obj(//create the query object)
Users.find(query).toList.flatMap(users =>
if(users.size > 0)
Future{Some(users)}
else
Future{None}
)
}
To be more specific I search all the document where the tags
field contain a document where the tag
field starts with category, but the tags
field doesn't contain any document where tag == product
.
How can i make that in mongodb??