Index on nested array elements

2,215 views
Skip to first unread message

KingCanadian

unread,
Jun 1, 2012, 2:13:35 PM6/1/12
to mongodb-user
Hello,

i want to index the value of a nested array (dateien.md5) - how can i
do this?
I have a Document like:
========================================
{
"dateien": [
{
"dateiname": "domino_20120529155008_0690012.jpg",
"md5": "6351f0c9ab076f16d12e6f77ec13e10d",
"bindata": "<Mongo Binary Data>"
}
],
"dokumentenart": "Einzugsermächtigung",
"dokumentenverz": "EZE_VS",
"erfassung_1_benid": "69",
"erfassung_1_tsp": "20120530082850",
"erfassung_1_zahlernummer": "31466308",
"erfassung_2_benid": "15",
"erfassung_2_tsp": "20120530112553",
"erfassung_2_zahlernummer": "31466308",
"import_benid": 10,
"scan_tsp": "20120529155049",
"seiten": 1,
"seitigkeit": 1,
"status": 5
}
========================================

Sometimes i have to search for the Value in dateien.md5
So i want to make a index for it with
.ensureIndex({dateien:1})

How can i see if the index was used when
findOne('dateien.md5'=>"6351f0c9ab076f16d12e6f77ec13e10d")
??


I tryed the explan command but there is no difference:

With Index ensured
========================================
Antwort vom Server:
{
"cursor": "BtreeCursor _id_ reverse",
"nscanned": 22477,
"nscannedObjects": 22477,
"n": 1,
"millis": 130,
"nYields": 0,
"nChunkSkips": 0,
"isMultiKey": false,
"indexOnly": false,
"indexBounds": {
"_id": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
},
"allPlans": [
{
"cursor": "BtreeCursor _id_ reverse",
"indexBounds": {
"_id": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
}
},
{
"cursor": "BasicCursor",
"indexBounds": [

]
}
],
"oldPlan": {
"cursor": "BtreeCursor _id_ reverse",
"indexBounds": {
"_id": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
}
}
}
========================================

With Index dropped
========================================
Antwort vom Server:
{
"cursor": "BtreeCursor _id_ reverse",
"nscanned": 22477,
"nscannedObjects": 22477,
"n": 1,
"millis": 132,
"nYields": 0,
"nChunkSkips": 0,
"isMultiKey": false,
"indexOnly": false,
"indexBounds": {
"_id": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
},
"allPlans": [
{
"cursor": "BtreeCursor _id_ reverse",
"indexBounds": {
"_id": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
}
},
{
"cursor": "BasicCursor",
"indexBounds": [

]
}
],
"oldPlan": {
"cursor": "BtreeCursor _id_ reverse",
"indexBounds": {
"_id": [
[
{
"$maxElement": 1
},
{
"$minElement": 1
}
]
]
}
}
}
========================================

Scott Hernandez

unread,
Jun 1, 2012, 2:33:45 PM6/1/12
to mongod...@googlegroups.com
You want to create this index on the field you want to query on:

ensureIndex({"dateien.md5":1})

See the docs: http://www.mongodb.org/display/DOCS/Indexes#Indexes-IndexingonEmbeddedFields%28%22DotNotation%22%29

> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb

KingCanadian

unread,
Jun 2, 2012, 9:21:28 AM6/2/12
to mongodb-user
Hello Scott,

yes - this works great. Thank you very much!

Regards,
Juergen


On 1 Jun., 20:33, Scott Hernandez <scotthernan...@gmail.com> wrote:
> You want to create this index on the field you want to query on:
>
> ensureIndex({"dateien.md5":1})
>
> See the docs:http://www.mongodb.org/display/DOCS/Indexes#Indexes-IndexingonEmbedde...
Reply all
Reply to author
Forward
0 new messages