Scan
Dynamoose provides the ability to scan a model by using the Model.scan
function. This function acts as a builder to construct your scan with the appropriate settings before executing it (scan.exec
).
Scan operations run on every item in your table or index. This means that all filtering is done after the items are read, and therefore is not the most performant method. Due to that, if possible, it is highly encouraged to use Model.query
as opposed to Model.scan
.
Model.scan([filter])
This is the basic entry point to construct a scan request. The filter property is optional and can either be an object or a string representing the key you which to first filter on. In the event you don't pass in any parameters and don't call any other methods on the scan object it will scan with no filters or options.
Cat.scan().exec() // will scan all items with no filters or options
Cat.scan("breed").contains("Terrier").exec() // will scan all items and filter all items where the key `breed` contains `Terrier`
Cat.scan({"breed": {"contains": "Terrier"}}).exec() // will scan all items and filter all items where the key `breed` contains `Terrier`
If you pass an object into Model.scan
the object for each key should contain the comparison type. For example, in the last example above, contains
was our comparison type. This comparison type must match one of the comparison type functions listed on this page.
Conditionals
On top of all of the methods listed below, every Scan
instance has all of the methods that a Condition
instance has. This means you can use methods like where
, filter
, eq
, lt
and more.
Please check the Condition documentation to find the rest of the methods you can use with Scan.
scan.exec([callback])
This will execute the scan you constructed. If you pass in a callback the callback function will be executed upon completion passing in an error (if exists), and the results array. In the event you didn't pass in a callback parameter, a promise will be returned that will resolve to the results array upon completion.
Cat.scan().exec((error, results) => {
if (error) {
console.error(error);
} else {
console.log(results);
// [ Item { name: 'Will', breed: 'Terrier', id: 1 },
// lastKey: undefined,
// count: 1,
// scannedCount: 2,
// timesScanned: 1 ]
console.log(results[0]); // { name: 'Will', breed: 'Terrier', id: 1 }
console.log(results.count); // 1
console.log(Array.isArray(results)); // true
console.log(results.scannedCount); // 2
}
});