Cursor Methods: Map

Transforming the documents of a query.

🪶 The map method allows you to transform the documents returned by a query.

1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.max.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "USER-DATABASE"
  • )
  • ;  
    6
  • var
  • userModel = merlin
  • .model(
  • "User", <userSchema>
  • )
  • ;
    7
    8
  • //cursor.map(<function>);
  • 9userModel
  • .find(
  • {
  • }
  • )
  • 10
  • .map(
  • doc =>
  • {
  • 11   return
  • {
  • age:
  • doc.age,
  • name:
  • doc.name
  • }
  • ;
    12
  • }
  • )
  • .then(
  • e =>
  • {
  • 13   console
  • .log(
  • e
  • )
  • ;
    14  
  • //returns:
  • 15   [
    16      
  • {
  • age:
  • 27
  • ,
  • name:
  • "Chris"
  • }
  • ,
    17      
  • {
  • age:
  • 25
  • ,
  • name:
  • "Sam"
  • }
  • ,
    18      
  • {
  • age:
  • -
  • 9
  • ,
  • name:
  • "Maya"
  • }
  • ,
    19   ]
    20
  • }
  • )