CRUD operations on MongoDB

This article describes CRUD operations on MongoDB. Create, Read, Update, Delete (CRUD) are the four basic storage functions for data storage.

Create

In mongodb the create function is called “insert()”. With the insert function you are able to store new documents.

# the following code stores a new document in the books collection
db.books.insert({'title': 'webdevelopment for dummies', 'isbn': 'a1234'})

Read

For reading data there are 2 functions available in mongodb: “find” and “findOne”. The find function can return any number of results, while findOne returns no or one result. 

# find -all- books of the author Stephen King
db.books.find({'author': 'Stephen King'})
# find *one* student with id 12 (if any exist)
db.students.findOne({'student_id': 12})

# find *one* student that lives in new york
# this example will return ONLY the first student
# even if there are more students that live in NY
db.students.findOne({'city': 'NY'})

 

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *