$elemMatch (query)
- $elemMatch
The $elemMatch operator matches more than one component within an array element. For example,
db.collection.find( { array: { $elemMatch: { value1: 1, value2: { $gt: 1 } } } } );
returns all documents in collection where the array array satisfies all of the conditions in the$elemMatch expression.
That is, where the value of value1 is 1 and the value of value2 is greater than 1. Matching arrays must have at least one element that matches all specified criteria. Therefore, the following document would not match the above query:
{ array: [ { value1:1, value2:0 }, { value1:2, value2:2 } ] }
while the following document would match this query:
{ array: [ { value1:1, value2:0 }, { value1:1, value2:2 } ] }
출처 - http://docs.mongodb.org/manual/reference/operator/query/elemMatch/
Here is the MongoDB query I am trying to reproduce:
Code:
db.myCollection.find({
$and: [
{'key.attributes': {$elemMatch: {'name': 'k1', 'value': 'kv1'}}},
{'key.attributes': {$elemMatch: {'name': 'k2', 'value': 'kv2'}}}
]
})
I have verified that this query produces the correct output using the mongo shell. I try to query it using spring data like this:
Code:
where("key.attributes").elemMatch(where("name").is("k1").and("value").is("kv1"))
.andOperator(
where("key.attributes").elemMatch(where("name").is("k2").and("value").is("kv2"))
);
However, this produces the following query:
Code:
{ "key.attributes" : { "$elemMatch" : { "name" : "k1" , "value" : "kv1"}} , "$and" : [ { "key.attributes" : { "$elemMatch" : { "name" : "k2" , "value" : "kv2"}}}]}
How can I get spring data to produce the query as shown above?
Problem solved!
Originally posted by
Andrei Tsibets View PostCode:
Query query = new Query(new Criteria().andOperator(
where("key.attributes").elemMatch(where("name").is("k1").and("value").is("kv1")),
where("key.attributes").elemMatch(where("name").is("k2").and("value").is("kv2"))
));
This approach worked for me. Thanks, Andrei!
I would never have thought to try constructing an empty Criteria object.
출처 - http://forum.spring.io/forum/spring-projects/data/nosql/127899-help-with-and-and-elemmatch-in-spring-data-mongodb
아래의 데이터를 참고해서 보자 user, actionList, somethingList, loc, date라는 구성으로 이루어진 단위이다. 여기서
actionList와 somethingList에 원하는 값을 가진 데이터만 추출하는 예시를 작성해보겠다.~!
{ "_id" : ObjectId("52c282c33004e2712c02ee89"),
"_class" : "com.gomp.trackingX.nosql.model.TXEvent",
"user" : { "mId" : 1, "userId" : "yhjung", "password" : "1234", "email" : "ㅁㅁㅁㅁㅁ", "nickname" : "hooni", "sex" : "MAN", "deviceOS" : "Android", "createDate" : "2013-12-07 12:10:55.0" },
"actionList" : [ { "mId" : 6, "name" : "사격" } ],
"somethingList" : ["mId" : 41, "name" : "아하하" ],
"loc" : { "longitude" : 61.966994763721516, "latitude" : 13.876642282331927 },
"date" : ISODate("2013-12-31T08:39:31.824Z") }
actionList와 somethingList를 보면 mId 값으로 데이터를 추출하는 코드이다. (Spring Data)
final Query query = new Query();
final List<Integer> actionIdList = new ArrayList<Integer>();
final List<Integer> someThingIdList = new ArrayList<Integer>();
리스트에 데이터 추가~~~~~
query.addCriteria(Criteria.where(TXEvent.KEY_ACTION_LIST).elemMatch(Criteria.where(Action.KEY_ID).in(actionIdList)));
query.addCriteria(Criteria.where(TXEvent.KEY_SOMETHING_LIST).elemMatch(Criteria.where(Something.KEY_ID).in(someThingIdList)));
this.mongoTemplate.find(query, 데이터클래스~~.class);
이해를 돕기위해 공식홈페이지 예제를 가져왔다. 아래의 데이터에서 원하는데이터를 추출할때~!
{
_id: 1,
zipcode: 63109,
students: [
{ name: "john", school: 102, age: 10 },
{ name: "jess", school: 102, age: 11 },
{ name: "jeff", school: 108, age: 15 }
]
}
{
_id: 2,
zipcode: 63110,
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 3,
zipcode: 63109,
students: [
{ name: "ajax", school: 100, age: 7 },
{ name: "achilles", school: 100, age: 8 },
]
}
{
_id: 4,
zipcode: 63109,
students: [
{ name: "barney", school: 102, age: 7 },
]
}
이렇게 검색을 하면 된다.
db.schools.find( { zipcode: 63109 },
{ students: { $elemMatch: { school: 102 } } } )
출처 - http://gomp.tistory.com/209