Casbah - 如何创建基于对象的查找?
Casbah - How to create a find based on object?
我正在创建一个接收对象的查找方法,对于每个不为空的属性,我需要在 MongoDBObject 上设置他的值。我在使用 $in 运算符时遇到问题。
这是我正在尝试做的事情:
def findRooms(room:Room) = {
val query = MongoDBObject.newBuilder
if(room.id != null && !room.id.isEmpty)
query+=("_id" -> room.id)
if(room.members != null && room.members.length > 0)
//Here i try to create a list to search inside the array
query+"members" $in List[MongoDBObject](room.members.map(mongoDBObject =>
parseUser(mongoDBObject)))
val result = roomCollection.find(query.result)
....
}
我的json里面的一个例子mongodb是:
{
"_id" : ObjectId("54bfccc92850e17166991765"),
"name" : "room1",
"members" : [{
"user" : {
"phoneNumber" : "12378954"
}
}, {
"user" : {
"email" : "test@google.com",
"phoneNumber" : "1010"
}
}]
}
{
"_id" : ObjectId("54bfccfb285033b1d718bc87"),
"name" : "room2",
"members" : [{
"user" : {
"phoneNumber" : "98476541"
}
}, {
"user" : {
"email" : "test@google.com",
"phoneNumber" : "1010"
}
}]
}
我想搜索数组中包含以下用户的所有文档:
{ user: { "phonenumber" : "12378954" }} and
{ user: { "email": "test@google.com", "phonenumber" : "1010" }}**
我如何在...中使用 query+="members"我应该创建什么对象来执行此搜索?
提前致谢。
编辑:
if(room.members != null && room.members.length > 0){
val emails = scala.collection.mutable.MutableList[String]()
val phones = scala.collection.mutable.MutableList[String]()
val forwardUris = scala.collection.mutable.MutableList[String]()
for(member <- room.members){
if(member.email != null && member.email != None)
emails+=member.email.get
if(member.forwardUrl != null && member.forwardUrl != None)
forwardUris+=member.forwardUrl.get
if(member.phoneNumber != null && member.phoneNumber != None)
phones+=member.phoneNumber.get
}
if(emails.length > 0)
query+=("members.user.email" -> MongoDBObject("$all" -> emails))
if(phones.length > 0)
query+=("members.user.phoneNumber" -> MongoDBObject("$all" -> phones))
if(forwardUris.length > 0)
query+=("members.user.forwardUrl" -> MongoDBObject("$all" -> forwardUris))
}
您需要使用 $all operator when querying arrays to only match documents where all items exist in the array. Then to query against a nested item in mongodb you need to use dot notation
所以在 shell 你会 运行:
db.test.find({"members.user.phoneNumber" : {$all: ["12378954", "1010"]},
"members.user.email": "test@google.com"})
在 Casbah 中:
val query: DBObject = MongoDBObject("members.user.phoneNumber" -> MongoDBObject("$all" -> List("12378954", "1010"),
"members.user.email" -> "test@google.com"))
roomCollection.find(query)
或者您可以使用 DSL:
("members.user.phoneNumber" $all ("1", "2")) ++
MongoDBObject("members.user.email" -> "test@google.com")
我正在创建一个接收对象的查找方法,对于每个不为空的属性,我需要在 MongoDBObject 上设置他的值。我在使用 $in 运算符时遇到问题。
这是我正在尝试做的事情:
def findRooms(room:Room) = {
val query = MongoDBObject.newBuilder
if(room.id != null && !room.id.isEmpty)
query+=("_id" -> room.id)
if(room.members != null && room.members.length > 0)
//Here i try to create a list to search inside the array
query+"members" $in List[MongoDBObject](room.members.map(mongoDBObject =>
parseUser(mongoDBObject)))
val result = roomCollection.find(query.result)
....
}
我的json里面的一个例子mongodb是:
{
"_id" : ObjectId("54bfccc92850e17166991765"),
"name" : "room1",
"members" : [{
"user" : {
"phoneNumber" : "12378954"
}
}, {
"user" : {
"email" : "test@google.com",
"phoneNumber" : "1010"
}
}]
}
{
"_id" : ObjectId("54bfccfb285033b1d718bc87"),
"name" : "room2",
"members" : [{
"user" : {
"phoneNumber" : "98476541"
}
}, {
"user" : {
"email" : "test@google.com",
"phoneNumber" : "1010"
}
}]
}
我想搜索数组中包含以下用户的所有文档:
{ user: { "phonenumber" : "12378954" }} and { user: { "email": "test@google.com", "phonenumber" : "1010" }}**
我如何在...中使用 query+="members"我应该创建什么对象来执行此搜索?
提前致谢。
编辑:
if(room.members != null && room.members.length > 0){
val emails = scala.collection.mutable.MutableList[String]()
val phones = scala.collection.mutable.MutableList[String]()
val forwardUris = scala.collection.mutable.MutableList[String]()
for(member <- room.members){
if(member.email != null && member.email != None)
emails+=member.email.get
if(member.forwardUrl != null && member.forwardUrl != None)
forwardUris+=member.forwardUrl.get
if(member.phoneNumber != null && member.phoneNumber != None)
phones+=member.phoneNumber.get
}
if(emails.length > 0)
query+=("members.user.email" -> MongoDBObject("$all" -> emails))
if(phones.length > 0)
query+=("members.user.phoneNumber" -> MongoDBObject("$all" -> phones))
if(forwardUris.length > 0)
query+=("members.user.forwardUrl" -> MongoDBObject("$all" -> forwardUris))
}
您需要使用 $all operator when querying arrays to only match documents where all items exist in the array. Then to query against a nested item in mongodb you need to use dot notation
所以在 shell 你会 运行:
db.test.find({"members.user.phoneNumber" : {$all: ["12378954", "1010"]},
"members.user.email": "test@google.com"})
在 Casbah 中:
val query: DBObject = MongoDBObject("members.user.phoneNumber" -> MongoDBObject("$all" -> List("12378954", "1010"),
"members.user.email" -> "test@google.com"))
roomCollection.find(query)
或者您可以使用 DSL:
("members.user.phoneNumber" $all ("1", "2")) ++
MongoDBObject("members.user.email" -> "test@google.com")