Basic NoSQL Operations

1. Retrieve Data

1.1 Basic retrieve Operation

Description: Retrieval in NoSQL databases often involves fetching documents or other data structures from collections or stores.

  • Example (using MongoDB):
db.customers.find({});

Explanation: This MongoDB command retrieves all documents from the customers collection.

1.2 Retrieve Distinct Values

Description: This operation is used to return unique values from a dataset.

  • Example (using MongoDB):
db.customers.distinct("country");

Explanation: Retrieves a list of unique country values from the customers collection.

1.3 Retrieve Limited Set of Data

Description: Similar to SQL’s SELECT TOP, this operation limits the number of records returned.

  • Example (using MongoDB):
db.customers.find({}).limit(10);

Explanation: Retrieves the first 10 documents from the customers collection.

2. Insert Data

2.1 Basic Insert Operation

Description: Inserting new data into a NoSQL database often involves adding documents to a collection.

  • Example (using MongoDB):
db.customers.insertOne({CustomerID: 1, Name: 'John Doe', Address: '123 Elm Street'});

Explanation: Inserts a new document into the customers collection with specified values.

2.2 Batch Insert

Description: Inserting multiple documents at once.

  • Example (using MongoDB):
db.customers.insertMany([
  {CustomerID: 2, Name: 'Jane Doe', Address: '456 Pine Street'},
  {CustomerID: 3, Name: 'Jim Beam', Address: '789 Maple Avenue'}
]);

Explanation: Inserts multiple customer records into the customers collection in a single operation.

3. Update Data

3.1 Basic Update Operation

Description: Modifying existing records in a NoSQL database.

  • Example (using MongoDB):
db.customers.updateOne(
  {CustomerID: 1},
  {$set: {Address: '456 Oak Street'}}
);

Explanation: Updates the address of the customer with CustomerID 1.

3.2 Update with Condition

Description: An update operation that uses a condition to determine which documents to update.

  • Example (using MongoDB):
db.customers.updateMany(
  {Country: 'USA'},
  {$set: {Status: 'Verified'}}
);

Explanation: Updates the status of all customers in the USA to ‘Verified’.

4. Delete Data

4.1 Basic Delete Operation

Description: Removing data from a NoSQL database.

  • Example (using MongoDB):
db.customers.deleteOne({CustomerID: 1});

Explanation: Deletes the customer with CustomerID 1 from the customers collection.

4.2 Delete with Condition

Description: Deleting documents based on a specific condition.

  • Example (using MongoDB):
db.customers.deleteMany({Country: 'USA'});

Explanation: Deletes all customers located in the USA.

These operations in NoSQL databases reflect similar functionalities as those in SQL but are adapted to fit the different data models and structures inherent in NoSQL systems. Understanding these basic operations is essential for effectively managing and manipulating data in NoSQL databases.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *