Model Methods: Find By Id And Delete

Finding a document by id and removing it from the collection in the database in MerlinDB.

🪶 The findByIdAndDelete method finds a document by its ID and removes it from the collection.

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
    7
  • const
  • UserSchema =
  • Schema(
  • {
  •   
    8  
  • name:
  • String
  • ,
    9  
  • age:
  • Number
  • 10
  • }
  • )
  • ;
    11
    12
  • var
  • userModel = merlin
  • .model(
  • "User", UserSchema
  • )
  • ;
    13
    14
  • //userModel.findById(<id>);
  • 15userModel
  • .findById(
  • "666cd78dTW96aW07a24c2958"
  • )
  • .then(
  • e =>
  • {
  • 16   console
  • .log(
  • e
  • )
  • ;
    17  
  • //returns
  • 18  
  • {
  • 19      
  • acknowledged:
  • true
  • ,
    20      
  • deletedCount:
  • 1
  • ,
    21      
  • data:
  • //...
  • 22  
  • }
  • 23
  • }
  • )
  • .catch(
  • e =>
  • {
  • 24   console
  • .log(
  • e
  • )
  • ;
    25  
  • //returns
  • 26  
  • {
  • 27      
  • acknowledged:
  • true
  • ,
    28      
  • deletedCount:
  • 0
  • 29  
  • }
  • 30
  • }
  • )