db.students.insertMany([
  { id: 1, name: 'Ryan', gender: 'M' },
  { id: 2, name: 'Joanna', gender: 'F' }
]);


  db.setProfilingLevel(1, { slowms: 100 });
  db.system.profile.find().pretty();
  
db.students.find({ gender: 'F' }); 

db.students.insert(
    
    {
    id:3,name:"Rahul",gender:"M"    
}
)

db.students.update({id:1},{$set:{email:'[email protected]'}})
db.students.update({id:2},{$set:{email:'[email protected]'}})
db.students.update({id:3},{$set:{email:'[email protected]'}})         

db.students.update({id:11},{$set:{name:"Sangita",gender:"F"}},{upsert:true})
    db.students.deleteOne({id:2})

db.marks.insertMany(
    [
      {sid:1,pass:"Failed",marks:32,ncc:'Y'},
      { sid:2,pass:'Passed',marks:40,ncc:'N'},
      { sid:3,pass:'Passed',marks:70,ncc:"Y"}
    ])
    
    

    db.marks.updateMany(
      {ncc:"Y"},
      {$set:{award:"yyes"}}
      )
      
db.students.aggregate([ 
    {$lookup:{ from:"marks",localField:"id",foreignField:"sid",as:"SMarks"}},
    {$project:{_id:0,SMarks:{_id:0}}}
    
])

db.marks.aggregate([
  {$group:{_id:null,Tsum:{$sum:"$marks"}}},
  {$project:{_id:0,Tsum:1}}
  ])
  
db.marks.aggregate([
  {$group:{_id:null,Tavg:{$avg:"$marks"}}}
  ])  
  
  db.marks.aggregate([
    {$group:{_id:null,Tmax:{$max:"$marks"},Tmin:{$min:"$marks"},TTsum:{$sum:"$marks"},TTavg:{$avg:"$marks"},Tcount:{$sum:1}}}
    ,
     {
    $addFields:{ empInfo: { $ifNull: ["$empInfo", [{ msg:"deleted by deletedOne" }]] } }
  },
    {$project:{_id:0}}
    //reshape
    ])
    
      db.students.replaceOne(
    { email: "[email protected]" },
    {
      name: "John Doe",
      age: 26,
      email: "[email protected]",
      address: {
        city: "New York",
        country: "USA"
      }
    }
  );


// db.students.find()
// db.marks.find({ncc:"Y"})
// db.marks.find({marks:{$gte:40}})
// db.marks.aggregate([
  
//   {$match:{ncc:{$in:["Y"]}}}
  
//   ])
  
// db.marks.aggregate([
  
//   {$match:{ncc:"N"}}
//     ])

db.mytags.insertMany([
  
  {
    "id": 1,
    "title": "Introduction to MongoDB",
    "tags": ["mongodb", "database"],
    "status":"active",
    "like":50
  },
  {
    "id": 2,
    "title": "Exploring NoSQL Concepts",
    "tags": ["nosql", "database"],
     "status":"inactive",
      "like":250
  },
  {
    "id": 3,
    "title": "Building Scalable Applications",
    "tags": ["php", "nosql", "scalability"],
     "status":"inactive",
      "like":1050
  },
  {
    "id": 4,
    "title": "Data Modeling in MongoDB",
    "tags": ["mongodb", "database", "modeling"],
     "status":"active",
      "like":350
  },
   {
    "id": 5,
    "title": "Node with MongoDB",
    "tags": ["nodejs", "sql", "react"],
     "status":"active",
      "like":"na"
  }

  ])
  
  // db.mytags.find({like:{$gt:500},status:{$eq:"active"}})
 // db.mytags.find({status:"inactive"})
  //db.mytags.find({ status: { $eq: "active" } });
  // db.mytags.aggregate([
  //   {$match:{status:"active",like:{$gt:51}}}
  //   ])
    
    // db.mytags.find({ title: { $regex: /^Building/ } });
    
    
//     db.mytags.find({
//   $or: [
//     { status: "active", tags: {$in:["mongodb"]} },
//     { like: { $gt: 1000 } }
//   ]
// });
 
// db.mytags.find({
//   $and:[
//     {status:"inactive"},
//     {like:{$lt:251}}
//     ]
// })
 
 //$nor
 
// db.mytags.find().sort({ like: -1 });
 
db.mytags.aggregate([
  { $unwind: "$status" }
]);
 
  db.mytags.aggregate([
    { $match: { like: { $gt: 50 } } }
  ]);
  
   db.mytags.aggregate([
    { $skip: 2 },
    { $limit: 3 }
  ]);
  
  
   db.mytags.aggregate([
    {
      $facet: {
        averageLike: [{ $group: { _id: null, avgLike: { $avg: "$like" } } }],
        totalLike: [{ $group: { _id: null, totalQty: { $sum: "$like" } } }]
      }
    }
  ]);
 
 
   db.mytags.countDocuments({ like: { $gt: 50 } });
 
  db.mytags.distinct("status");
  db.mytags.distinct("tags");
 
 // db.mytags.find({ like: { $type: "string" } });
 
 
  //db.mytags.find({ products: { $elemMatch: { quantity: { $gt: 10 } } } });
 
   
   //db.mytags.find({tags:{$in:["mongodb","database"]}})
    
     //db.mytags.find({tags: { $all: ["mongodb", "database"] } });
     
by

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'})