Model Methods: Find By Id
Finding a document by id in the database collection in MerlinDB.
🪶 The findById method finds a document based on its ID in 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( ) ;5 merlin.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>, <options>); 15 userModel.findById( "666cd78dTW96aW07a24c2958", { 16 17 fields: { name: 1 , age: 1 } , 18 //Returns only the 'name' and 'age' fields; 19 20 fields: { id_: -1 } 21 //Returns all fields except the 'id' field; 22 23 } ) .then( e => console.log( e) )