Last active
August 29, 2015 14:14
-
-
Save golbin/1552080d438d23135122 to your computer and use it in GitHub Desktop.
MongoDB Generic Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 더미 데이터 생성 | |
for (var i = 0; i < 100000; ++i) { | |
var arr = []; | |
var size = Math.floor(Math.random() * 10); | |
for (var j = 0; j < size; ++j) { | |
var doc = {}; | |
doc["prop" + j] = Math.floor(Math.random() * 1000); | |
arr.push(doc); | |
} | |
db.test.insert({ | |
props: arr, | |
activities: [ | |
{ | |
"vote": Math.floor(Math.random() * 1000) | |
}, | |
{ | |
"comment": Math.floor(Math.random() * 100) | |
} | |
], | |
contents: [ | |
{ | |
"title": "제목" | |
}, | |
{ | |
"contents": "내용" | |
} | |
] | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 인덱스 추가 | |
db.test.ensureIndex({"props": 1}, {sparse: true}); | |
db.test.ensureIndex({"activities": 1}, {sparse: true}); | |
// 기본 검색 | |
db.test.find({"props": {"prop0": 0}}).skip(50).limit(3); | |
db.test.find({"props": {"prop0": 0}}).skip(50).limit(3).explain(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find로는 임베디드 엘리먼트를 검색할 때는 소팅이 안먹으므로, aggregate를 이용해야한다. | |
db.test.aggregate([ | |
{$match: {"props": {"prop0": 0}}}, | |
{$sort: {"activities.vote": -1}}, | |
{$skip: 50}, | |
{$limit: 3} | |
]); | |
// 쿼리에 조건식 넣기 | |
db.test.aggregate([ | |
{$match: {"props": {$in: [{"prop0": 0}, {"prop0": 1}]}}}, | |
{$sort: {"activities.vote": -1}} | |
]); | |
db.test.aggregate([ | |
{$match: {"props": {$all: [{"prop0": 0}, {"prop1": 50}]}}}, | |
{$sort: {"activities.vote": -1}} | |
]); | |
// 조금 더 복잡한 쿼리 | |
db.test.aggregate([ | |
{$match: {$and: [ | |
{"props": {$in: [{"prop0": 0}, {"prop0": 1}]}}, | |
{"props": {$in: [{"prop1": 1}, {"prop1": 2}]}} | |
]}}, | |
{$sort: {"activities.vote": -1}}, | |
{$project: { | |
"_id": "$_id", | |
"prop": "$props.prop0", | |
"vote": "$activities.vote", | |
"contents": "$contents" | |
}}, | |
{$skip: 0}, | |
{$limit: 3} | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_id": "54d035ac8b3ce67ad959c721", | |
"created_at": "2015-02-05T02:31:33.462Z", | |
"prop": [ | |
{ | |
"type": 123 | |
}, | |
{ | |
"user_id": 345 | |
} | |
... | |
], | |
"activities": [ | |
{ | |
"vote": 10 | |
}, | |
{ | |
"comment": 123 | |
} | |
... | |
], | |
"contents": [ | |
{ | |
"title": "제목" | |
}, | |
{ | |
"contents": "내용" | |
} | |
... | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment