performance - MongoDB Complex Subdocument Query -


I have a collection that has 100,000 documents with multiple nested arrays, query me at the lowest level based on an asset Need and return the bus object to the bottom of the array.

Document structure:

  {_id: 12345, type: "employee", people: [{name: "rob", item: [[item: name: " Robseitman ", value:" $ 10.00 ", Description:" Some details about item "}, {itemName:" RobsItemTwo ", value:" $ 15.00 ", Description:" Some details about item "}]}] }  

I am using the aggregation pipeline to achieve the expected result of the work, although the performance is very awesome here is my query:

  Db.collection.aggregate ([{$ match: {"type": "employee"}}, {$ Unwind: "$ people"}, {$ unwind: "$ people.items"}, {$ match: {$ or: [// This $ match {{people.items.itemName "} can contain dozens of items "RobsItemOne"}, {"people.items .itemName": "RobsItemTwo"}]}}, {$ Project: {_id: 0, // This is due to system ID: "$ _id", Type: "$ type", item: "$ people items.itemName", value: "$ people.items.value"}}, {$ out: tempCollection} // To avoid this, but more than the maximum document size Results are:  

The results are:

<"employee", "SystemID": 12345, "item": "robysememan", "value": "$ 10.00"} , {"Type": "A What can I do to speed up this query: "Employees", "System ID": 12345, "Items": "RobsItemTwo", "value": "$ 10.00"}]

? I have tried to use indexed, but the last index of the Mongo docks, the initial $ match, is ignored.

What else can you try, that $ unwind people < / P>

  ... open {$: "$ people"}, {$ match: {"people.items.itemName": {$ in: ["RobsItemOne", "RobsItemTwo"]}} }, <$ Unwind: "$ people.items"}, ....  

This is the following $ unwind and $ match The number of records asked by operators will bring down.

Since you have a large number of records, you can use the {allowDiskUse: true} option.

Enables writing for temporary files when set to true, then the aggregation phase can write data in the dbpath directory in the _tmp subdirectory.

Then, your last query wants:

  db ($ {Match}: {$ $ match: {"type": "employee" }}, {$ Unwind: "$ people"}, {$ match: {"people.items.itemName": {$ in: ["RobsItemOne", "RobsItemTwo"]}}}, {$ unwind: "$ people .items "}, {$ match: {$ or: [// This $ match {{people.items can contain dozens of items .itemName": "RobsItemOne"}, {"people.items.itemName": "RobsItemTwo"}]}}, {$ project: {_id: 0, // This is because of system ID: "$ _id", type: "$ type", item: "$ people.items.itemName ", Value:" $ people.items.value "}}], {allo WDiskUse: true})  

Comments