MongoDB | Advanced Update Operators

db.users.updateOne({"_id" : ObjectId("11a")}, {$inc: {age: 2}}) Increment age by 2 for object id 11a db.users.updateOne({"_id" : ObjectId("11a")}, {$inc: {age: -1}, $set: {exp: 22}}) Decrement age by 1 for object id 11a and set exp = 22 let assume a user Chris has age of 35 db.users.updateOne({name: "Chris"}, {$min: {age: 32}}) If age is more than 32, set age to 32. Chris age will change to 32 db.users.updateOne({name: "Chris"}, {$max: {age: 38}}) If age is less than 38, set age to 38....

September 29, 2021 · Kunal Gautam

MongoDB | Update Operators

Fields Name Description $currentDate Sets the value of a field to current date, either as a Date or a Timestamp. $inc Increments the value of the field by the specified amount. $min Only updates the field if the specified value is less than the existing field value. $max Only updates the field if the specified value is greater than the existing field value. $mul Multiplies the value of the field by the specified amount....

September 28, 2021 · Kunal Gautam

MongoDB | Advanced Read Operations

db.users.find({}).sort({id: 1}).pretty() .sort() with id in acending order db.users.find({}).sort({id: -1}).pretty() .sort() with id in decending order db.users.find({}).sort({"ratings.average": -1}).pretty() .sort() filter applied on average doocument/element under ratings with decending order db.users.find({}).sort({"ratings.average": 1},{runtime: -1}).pretty() .sort() in acending order by average ratings followed by runtime in decending order. eg. result will be like: 1) avgrating: 2, runtime 60 2) avgrating: 2, runtime 40 3) avgrating: 2, runtime 20 4) avgrating: 4.1, runtime 60 5) avgrating: 5, runtime 50 6) avgrating: 5, runtime 40 7) avgrating: 7....

September 27, 2021 · Kunal Gautam

MongoDB | Read Operation with Arrays

db.MoviesCollection.find({genres: {$size: 3}}) Find all generes which has size of 3 elements/documents db.movieStarts.find({genre: { $all: ["action", "thriller"]}}, {title: 1, genre: 1, _id:0 }) Find all generes having action and thriller element. If $all is not used, it will search for the particular order and which is not what we might want. db.users.find({ hobbies: { $elemMatch: { title: 'sports', frequency: { $gte: 3 } } } }).pretty() Find all hobbies with element having sports and frequency Greater than or eqals to 3....

September 26, 2021 · Kunal Gautam

MongoDB | Recap: Document, Collection, Methods, Filter and Operators.

September 25, 2021 · Kunal Gautam

MongoDB | Query and Projections

Query Selectors Comparison For comparison of different BSON type values, see the specified BSON comparison order. Name Description $eq Matches values that are equal to a specified value. $gt Matches values that are greater than a specified value. $gte Matches values that are greater than or equal to a specified value. $in Matches any of the values specified in an array. $lt Matches values that are less than a specified value....

September 25, 2021 · Kunal Gautam

MongoDB | Exploring Insert Option

db.tableName.insertMany([{_id: 1, name: 1}, {_id: 2, name: 2}, {_id: 3, name: 3}]) This will insert documents. In case let assume name:2 is already in database, it will insert name:1 and throw error. the name: 3 won’t be inserted in default behaviour. To get new document inserted and skip the already inserted documents, we can use: db.tableName.insertMany([{_id: 1, name: 1}, {_id: 2, name: 2}, {_id: 3, name: 3}], {ordered: false}) The argument ordered: false will insert document which are not present in the document....

September 24, 2021 · Kunal Gautam

MongoDB | Data Types & Limits

Data Types & Limits MongoDB has a couple of hard limits - most importantly, a single document in a collection (including all embedded documents it might have) must be <= 16mb. Additionally, you may only have 100 levels of embedded documents. You can find all limits (in great detail) here: https://docs.mongodb.com/manual/reference/limits/ For the data types, MongoDB supports, you find a detailed overview on this page: https://docs.mongodb.com/manual/reference/bson-types/ Important data type limits are:...

September 23, 2021 · Kunal Gautam

MongoDB | Datatypes

MongoDB supports many datatypes. 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....

September 22, 2021 · Kunal Gautam

MongoDB | CRUD Operations

1) Creating Record: Record can be inserted using: db.tableName.insert() db.tableName.insertOne() db.tableName.insertMany() .insert() can accept single or multiple records. Returns WriteResult/BulkWriteResult. Example showing inserting one record db.tableName.insert( { name: "John", age: 23, skills: { programming: "PHP, Java, Python", database: "MongoDB, SQLITE" } } ) Example showing Multiple Records db.inventory.insert([ { item: "journal", qty: 25, status: "A", size: { h: 14, w: 21, uom: "cm" }, tags: [ "blank", "red" ] }, { item: "notebook", qty: 50, status: "A", size: { h: 8....

September 21, 2021 · Kunal Gautam