Articles on Trending Technologies

Technical articles with clear explanations and examples

Modify sequence in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

To modify a sequence in MongoDB, use findAndModify() method to atomically increment a counter value. This approach creates auto-incrementing sequence numbers for document IDs. Syntax db.collection.findAndModify({ query: {_id: "sequenceName"}, update: {$inc: {sequence_value: 1}}, new: true }); Create Sample Data First, create a collection with an initial document ? db.demo261.insertOne({_id:100, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 100 } db.demo261.find(); { "_id" : 100, "Name" : "Chris" } Example: Create ...

Read More

Get MongoDB documents that contains specific attributes in array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 578 Views

To get MongoDB documents that contain specific attributes in an array, use $and along with dot notation to match multiple field values within the same array field. Syntax db.collection.find({ $and: [ {"arrayField.attribute1": "value1"}, {"arrayField.attribute2": "value2"} ] }); Sample Data Let us first create a collection with documents − db.demo2.insertMany([ { "StudentInformation": [ ...

Read More

How to run MongoDB shell using mongos command?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 828 Views

The mongos command is used to start a MongoDB router process in sharded cluster deployments. It acts as an interface between client applications and the sharded cluster, routing queries to the appropriate shards. Syntax mongos --configdb /, Basic mongos Command To start the mongos router, specify the configuration replica set ? mongos --configdb configReplSet/config1.example.com:27019, config2.example.com:27019, config3.example.com:27019 Common mongos Options Here are the most frequently used mongos command options ? mongos --configdb configReplSet/config1:27019, config2:27019 --port 27017 mongos --configdb configReplSet/config1:27019, config2:27019 --logpath /var/log/mongodb/mongos.log mongos --configdb configReplSet/config1:27019, config2:27019 --fork --logpath ...

Read More

How to update % printed to Console from MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 169 Views

To update and print percentage values to console from MongoDB script, create a variable with your numeric value and use the print() method with toFixed() to format decimal places. Syntax var variableName = numericValue; print(variableName.toFixed(decimalPlaces) + " %"); Example Create a variable with a percentage value and format it to 2 decimal places ? var amount = 10.58945; print(amount.toFixed(2) + " %"); The output of the above code is ? 10.59 % Key Points Use toFixed() to control the number of decimal places displayed. ...

Read More

Invoke convertToCapped and convert an existing collection to capped in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

To convert an existing collection to capped in MongoDB, use the convertToCapped command with runCommand(). A capped collection has a fixed size and automatically removes old documents when the size limit is reached. Syntax db.runCommand({ convertToCapped: "collectionName", size: sizeInBytes }); Sample Data Let us create a regular collection with some documents ? db.demo260.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"} ]); { "acknowledged": true, ...

Read More

MongoDB inverse of query to return all items except specific documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 617 Views

To get documents except some specific documents, use $nor along with $and. The $nor operator returns documents that do not match any of the specified conditions, making it perfect for inverse queries. Syntax db.collection.find({ $nor: [ { $and: [{ field1: value1 }, { field2: value2 }] } ] }); Sample Data Let us first create a collection with sample student documents — db.demo1.insertMany([ { "StudentName": "Chris", "StudentMarks": 38 }, ...

Read More

How to specify the order in which the query returns matching documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 141 Views

To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is obtained from db.collectionName.find(). Syntax db.collection.find().sort({ field: 1 }); // Ascending order db.collection.find().sort({ field: -1 }); // Descending order Where 1 means ascending order and -1 means descending order. Sample Data Let us create a collection with documents − db.demo259.insertMany([ { "Subject": "MySQL" }, { "Subject": "Java" }, { "Subject": "MongoDB" } ]); { ...

Read More

How to pull value from array of ObjectIDs in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 744 Views

To pull value from array of ObjectIDs in MongoDB, use the $pull operator with the $in operator to match and remove specific ObjectID values from the array. Syntax db.collection.update( { /* query */ }, { $pull: { arrayField: { $in: [ObjectId("id1"), ObjectId("id2")] } } } ); Sample Data Let us create a collection with documents ? db.demo258.insertOne({ "arrayOfObjectsId": [ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91") ...

Read More

Set MongoDB compound index with a fixed value field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 244 Views

A MongoDB compound index with a fixed value field ensures uniqueness across multiple fields, including fields that may be missing (null) in some documents. When you create a unique compound index, MongoDB treats missing fields as null values. Syntax db.collection.createIndex( { "field1": 1, "field2": 1 }, { unique: true } ); Create Sample Data First, let's create a unique compound index on StudentName and StudentAge ? db.compoundIndexDemo.createIndex( {"StudentName": 1, "StudentAge": 1}, {unique: true} ); ...

Read More

Update MongoDB variable value with variable itself?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 561 Views

To update a MongoDB field value using its current value as part of the new value, use the $set operator with string concatenation or the $concat aggregation operator within an update pipeline. Syntax // Using aggregation pipeline (recommended) db.collection.updateMany( {}, [{ $set: { fieldName: { $concat: ["$fieldName", " additional text"] } } }] ); // Using $set with literal string (replaces with literal text) db.collection.update( {}, { $set: { fieldName: "fieldName additional text" } } ); Sample Data ...

Read More
Showing 23261–23270 of 61,297 articles
Advertisements