MongoDB CRUD Operation

MongoDB CRUD Operation

·

2 min read

Table of contents

Create

  • db.people.insert({name: 'Harry', age: 25});

    or

  • db.people.save({name: 'Harry', age: 25});

  • The difference with save is that if the passed document contains an _id field, if a document already exists with that _id it will be updated instead of being added as new.

  • Two new methods to insert documents into a collection, in MongoDB 3.2.x: - Use insertOne to insert only one record:

  • db.people.insertOne({name: 'Harry', age: 25});

  • Use insertMany to insert multiple records:

  • db.people.insertMany([{name: 'Harry', age: 25},{name: 'John', age: 25}, {name: 'Jerry', age: 20}])

Read

  • The query for all the docs in the people collection that have a name field with a value of 'Harry':

  • db.people.find({name: 'Harry'})

  • Or just the first one:

  • db.people.findOne({name: 'Harry'})

  • You can also specify which fields to return by passing a field selection parameter. The following will exclude the _id field and only include the age field: db.people.find({name: 'Harry'}, {_id: 0, age: 1})

  • If you want to find a sub-record like an address object containing country, city, etc.

  • db.people.find({'address.country': 'US'})

  • & specify field too if required

  • db.people.find({'address.country': 'US'}, {'name': true, 'address.city': true})

Update

  • Update the entire object:

  • db.people.update({name: 'Harry'}, {age: 29, name: 'Harry'})

  • db.people.updateOne({name: 'Harry'},{age: 29, name: 'Harry'}) //Will replace only the first matching document.

  • db.people.updateMany({name: 'Harry'},{age: 29, name: 'Harry'}) //Will replace all matching documents.

  • Or just update a single field of a document. In this case age: db.people.update({name: 'Harry'}, {$set: {age: 29}})

  • You can also update multiple documents simultaneously by adding a third parameter. This query will update all documents where the name equals Harry:

  • db.people.update({name: 'Harry'}, {$set: {age: 29}}, {multi: true})

  • db.people.updateOne({name: 'Harry'},{$set:{age: 30}) //Will update only the first matching document.

  • db.people.updateMany({name: 'Harry'},{$set:{age: 30}}) //Will update all matching documents.

  • If a new field is coming for the update, that field will be added to the document. db.people.updateMany({name: 'Harry'},{$set:{age: 30, salary:50000}})// Document will have salary a field as well. If a document is needed to be replaced, db.collection.replaceOne({name:'Harry'}, {name:'Lakmal',age:25,address:'Sri Lanka'}) can be used.

Delete

  • Deletes all documents matching the query parameter:

  • db.people.deleteMany({name: 'Harry'})

  • db.people.remove({name: 'Harry'})

  • Or just one

  • db.people.deleteOne({name: 'Harry'})

  • db.people.remove({name: 'Harry'}, true)

  • MongoDB's remove() method. If you execute this command without any argument or without an empty argument it will remove all documents from the collection.

  • db.people.remove();

  • or

  • db.people.remove({});

Did you find this article valuable?

Support Inspriom's by becoming a sponsor. Any amount is appreciated!