// 1) Write a query to change the book copies to 2800 and increase the book price by 3000 for the BookCategory "Action and Adventure".
// 2) Display all the documents in the books collection.

// Collection Name: books

// The structure of the document is given below:
// {

//         "_id" : 1,

//         "BookId" : "ISBN:B001012:29836",

//         "BookTitle" : "Absalom, Absalom!",

//         "BookAuthor" : "William Faulkner",

//         "BookCategory" : "Action and Adventure",

//         "BookPrice" : 2387,

//       "BookCopies":4000,

//         "PublishDate" : ISODate("1931-12-30T00:00:00Z")

// }


db.employees.updateOne([{BookCategory:'Action and Adventure'},{$inc:{BookCopies:2800},{BookPrice:3000}}]);
db.employees.find(); 

MongoDB online editor

Write, Run & Share MongoDB queries online using OneCompiler's MongoDB online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MongoDB. Getting started with the OneCompiler's MongoDB editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MongoDB' and start writing queries to learn and test online without worrying about tedious process of installation.

About MongoDB

MongoDB is a cross platform document oriented NoSQL database.

Key Features:

  • Designed to overcome the the limitations of relational databases approach and other NoSQL solutions
  • Horizontal scaling
  • Load balancing capabilities
  • Better data availability and stability

Syntax help

Commands

Inserting documents

  1. db.collection.insert(): Using insert you can either insert one document or array of documents
db.employees.insert(   {empId: 3, name: 'Ava', dept: 'Sales' });
  1. db.collection.insertOne(): Inserts one document
db.employees.insertOne(  {empId: 4, name: 'Nick', dept: 'Accounting' });
  1. db.collection.insertMany: Inserts multiple documents
db.employees.insertMany([
  {empId: 1, name: 'Clark', dept: 'Sales' },
  {empId: 2, name: 'Dave', dept: 'Accounting' }
]);

Updating documents

  1. db.collection.update() : Updates one or more than one document(s) in collection based on matching document and based on multi option
db.employees.update(   
  {empId: 3 },
  { $set: { region: "Asia" } }
);
  1. db.collection.updateOne() : Updates a single document in collection based on matching document
db.employees.updateOne(   
  {empId: 2 },
  { $set: { region: "Asia" } }
);
  1. db.collection.updateMany() : Updates multiple documents in collection based on the condition.
db.employees.updateMany(   
  { dept: 'Sales'},
  { $set: { region: "US" } }
);

Deleting documents

  1. db.collection.deleteOne(<filter>, <options>): Deletes a Single document from collection
db.employees.deleteOne({ empId: 1})
  1. db.collection.deleteMany(<filter>, <options>): Deletes all documents with matching filter
db.employees.deleteMany({ dept: 'Sales'})