Header Ads Widget

MongoDB

MongoDB:

Introduction:

MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you a great understanding of MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database.

Data Types:

MongoDB supports many data types. Some of them are −

  • String − This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid.
  • Integer − This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
  • Boolean − This type is used to store a boolean (true/ false) value.
  • Double − This type is used to store floating-point values.
  • Min/ Max keys − This type is used to compare a value against the lowest and highest BSON elements.
  • Arrays − This type is used to store arrays or lists or multiple values into one key.
  • Timestamp − timestamp. This can be handy for recording when a document has been modified or added.
  • Object − This data type is used for embedded documents.
  • Null − This type is used to store a Null value.
  • Symbol − This datatype is used identically to a string; however, it's generally reserved for languages that use a specific symbol type.
  • Date − This data type is used to store the current date or time in UNIX time format. You can specify your own date time by creating an object of Date and passing day, month, a year into it.
  • Object ID − This data type is used to store the document’s ID.
  • Binary data − This data type is used to store binary data.
  • Code − This data type is used to store JavaScript code into the document.
  • Regular expression − This data type is used to store regular expression.

Creating Document:

Insert a Single Document

db.collection.insertOne() inserts a single document into a collection.

Insert Multiple Document

db.collection.insertMany() can insert multiple documents into a collection. Pass an array of documents to the method.

 

Updating Document

db.collection.updateOne(<filter>, <update>, <options>)

Updates at most a single document that matches a specified filter even though multiple documents may match the specified filter.

db.collection.updateMany(<filter>, <update>, <options>)

Update all documents that match a specified filter.

db.collection.replaceOne(<filter>, <update>, <options>)

Replaces at most a single document that matches a specified filter even though multiple documents may match the specified filter.

db.collection.update()

Either updates or replaces a single document that matches a specified filter or updates all documents that match a specified filter.

By default, the db.collection.update() method updates a single document. To update multiple documents, use the multi option.

Deleting Documents

db.collection.deleteMany()

Delete all documents that match a specified filter.

db.collection.deleteOne()

Delete at most a single document that matches a specified filter even though multiple documents may match the specified filter.

db.collection.remove()

Delete a single document or all documents that match a specified filter.

db.collection.findOneAndDelete()
findOneAndDelete() provides a sort option. The option allows for the deletion of the first document sorted by the specified order.

db.collection.findAndModify()
db.collection.findAndModify() provides a sort option. The option allows for the deletion of the first document sorted by the specified order.

db.collection.bulkWrite()

Post a Comment

0 Comments