Guest User

Untitled

a guest
Nov 13th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. // Creating a new document
  2. let ciaran = new User({
  3. username: "ciaran",
  4. password: "secure"
  5. })
  6. ciaran.save()
  7. .then(myNewDbObject => console.log('Success!')
  8. .catch(err => console.log('Oops', err)
  9.  
  10. // Find everything in a collection
  11. User.find().then(users => console.log(users))
  12.  
  13. // Find one document
  14. User.findOne({name: "ciaran"})
  15. .then(user => console.log(user)
  16.  
  17. // Update a document - can also findByIdAndUpdate(id)
  18. User.findOneAndUpdate(
  19. {name: "ciaran"}, // condition to find a record by
  20. {age: 21}, // update to apply
  21. {new: true} // return the new record
  22. ).then(updatedUser => console.log(updatedUser))
  23.  
  24. // Delete a document
  25. User.findOneAndRemove({condition: value})
Add Comment
Please, Sign In to add comment