db.createCollection('orders') db.orders.insertMany([ { _id: 1, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-01"), price: 25, items: [ { sku: "oranges", qty: 5, price: 2.5 }, { sku: "apples", qty: 5, price: 2.5 } ], status: "A" }, { _id: 2, cust_id: "Ant O. Knee", ord_date: new Date("2020-03-08"), price: 70, items: [ { sku: "oranges", qty: 8, price: 2.5 }, { sku: "chocolates", qty: 5, price: 10 } ], status: "A" }, { _id: 3, cust_id: "Busby Bee", ord_date: new Date("2020-03-08"), price: 50, items: [ { sku: "oranges", qty: 10, price: 2.5 }, { sku: "pears", qty: 10, price: 2.5 } ], status: "A" }, { _id: 4, cust_id: "Busby Bee", ord_date: new Date("2020-03-18"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }, { _id: 5, cust_id: "Busby Bee", ord_date: new Date("2020-03-19"), price: 50, items: [ { sku: "chocolates", qty: 5, price: 10 } ], status: "A"}, { _id: 6, cust_id: "Cam Elot", ord_date: new Date("2020-03-19"), price: 35, items: [ { sku: "carrots", qty: 10, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" }, { _id: 7, cust_id: "Cam Elot", ord_date: new Date("2020-03-20"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }, { _id: 8, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 75, items: [ { sku: "chocolates", qty: 5, price: 10 }, { sku: "apples", qty: 10, price: 2.5 } ], status: "A" }, { _id: 9, cust_id: "Don Quis", ord_date: new Date("2020-03-20"), price: 55, items: [ { sku: "carrots", qty: 5, price: 1.0 }, { sku: "apples", qty: 10, price: 2.5 }, { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" }, { _id: 10, cust_id: "Don Quis", ord_date: new Date("2020-03-23"), price: 25, items: [ { sku: "oranges", qty: 10, price: 2.5 } ], status: "A" } ]) db.orders.mapReduce( function() { emit(this.cust_id, this.price); }, function(keyCustId, valuesPrices) { return Array.sum(valuesPrices); }, { out: "map_reduce_example" } ) db.map_reduce_example.find().sort( { _id: 1 } )
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.
MongoDB is a cross platform document oriented NoSQL database.
db.collection.insert()
: Using insert
you can either insert one document or array of documentsdb.employees.insert( {empId: 3, name: 'Ava', dept: 'Sales' });
db.collection.insertOne()
: Inserts one documentdb.employees.insertOne( {empId: 4, name: 'Nick', dept: 'Accounting' });
db.collection.insertMany
: Inserts multiple documentsdb.employees.insertMany([
{empId: 1, name: 'Clark', dept: 'Sales' },
{empId: 2, name: 'Dave', dept: 'Accounting' }
]);
db.collection.update()
: Updates one or more than one document(s) in collection based on matching document and based on multi
optiondb.employees.update(
{empId: 3 },
{ $set: { region: "Asia" } }
);
db.collection.updateOne()
: Updates a single document in collection based on matching documentdb.employees.updateOne(
{empId: 2 },
{ $set: { region: "Asia" } }
);
db.collection.updateMany()
: Updates multiple documents in collection based on the condition.db.employees.updateMany(
{ dept: 'Sales'},
{ $set: { region: "US" } }
);
db.collection.deleteOne(<filter>, <options>)
: Deletes a Single document from collectiondb.employees.deleteOne({ empId: 1})
db.collection.deleteMany(<filter>, <options>)
: Deletes all documents with matching filterdb.employees.deleteMany({ dept: 'Sales'})