Smita Kapse

Smita Kapse

388 Articles Published

Articles by Smita Kapse

Page 12 of 39

How to do alphanumeric sort in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 627 Views

To perform alphanumeric sort in MongoDB, use the collation method with numericOrdering: true. This ensures that numeric portions within strings are sorted numerically rather than lexically (e.g., "STU101" comes before "STU1010"). Syntax db.collection.find().sort({field: 1}).collation({ locale: "en_US", numericOrdering: true }); Sample Data db.alphanumericSortDemo.insertMany([ {"StudentId": "STU1010"}, {"StudentId": "STU1101"}, {"StudentId": "STU1901"}, {"StudentId": "STU908"}, {"StudentId": "STU101"} ]); Display all documents to see the insertion order ? ...

Read More

Reverse array field in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 342 Views

To reverse array field in MongoDB, you can use the forEach() method to iterate through documents and manually reorder array elements using the $set operator. Syntax db.collection.find().forEach(function (document) { var reversedArray = document.arrayField.reverse(); db.collection.update(document, { $set: {arrayField: reversedArray } }); }); Sample Data Let us first create a collection with documents ? db.reverseArrayDemo.insertOne({"Skills":["C", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ccddf99dceb9a92e6aa1946") } Following is the query to display all documents from ...

Read More

How to increment a field in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 456 Views

To increment a field in MongoDB, use the $inc operator with the update() method. The $inc operator increases the numeric value of a field by a specified amount. Syntax db.collection.update( { "query": "criteria" }, { $inc: { "fieldName": incrementValue } } ); Sample Data Let us create a collection with player scores ? db.incrementDemo.insertMany([ { "PlayerScore": 100 }, { "PlayerScore": 290 }, { "PlayerScore": 560 } ]); { ...

Read More

Remove and update the existing record in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 199 Views

To remove and update existing records in MongoDB, use the $pull operator which removes elements from an array that match specified criteria. This operator effectively updates the document by removing the matching array elements. Syntax db.collection.update( { "filter_condition": "value" }, { "$pull": { "arrayField": { "field": "valueToRemove" } } } ); Sample Data Let us create a collection with documents containing user details in an array ? db.removeDemo.insertMany([ { "UserName": "Larry", ...

Read More

How do I know which MongoDB version is installed using the Command Line?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 12K+ Views

To check which MongoDB version is installed on your system, you can use the command line to query the MongoDB server directly. This is useful for verifying your installation and ensuring compatibility with your applications. Syntax mongo --version Or alternatively: mongod --version Method 1: Using mongo --version Open your command prompt and navigate to the MongoDB bin directory. Then execute the following command ? mongo --version MongoDB shell version v4.0.5 git version: 3739429dd92b92d1b0ab120911a23d50bf03c412 OpenSSL version: OpenSSL 1.1.0i 14 Aug 2018 allocator: tcmalloc modules: none ...

Read More

Retrieve the position in an Array in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 370 Views

To retrieve the position of an element in an array in MongoDB, you can use the map-reduce approach with JavaScript's indexOf() method. This technique allows you to find the index position of any array element. Syntax db.collection.mapReduce( function() { emit(this._id, { "IndexValue": this.arrayField.indexOf("searchValue") }); }, function() {}, { "out": { "inline": 1 }, "query": { "arrayField": "searchValue" ...

Read More

How to create a user in MongoDB v3?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 252 Views

To create a user in MongoDB v3, use the createUser() method. This allows you to create a user by specifying the username, password, and roles that assign specific permissions to databases. Syntax use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourPermission", db: "yourDatabase" } ] } ); Example Let us create a user named "Robert" with readWrite permissions ...

Read More

Use result from MongoDB in shell script?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 598 Views

To use MongoDB query results in shell scripts, you can store the result in a JavaScript variable using the var keyword and then manipulate or access the data as needed. Syntax var variableName = db.collection.findOne(query, projection); variableName.fieldName Sample Data Let us first create a collection with a document − db.useResultDemo.insertOne({"StudentFirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda70f5b50a6c6dd317adcd") } Display all documents from a collection with the help of find() method − db.useResultDemo.find(); { "_id" : ObjectId("5cda70f5b50a6c6dd317adcd"), ...

Read More

How does MongoDB order their docs in one collection?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 131 Views

MongoDB orders documents in a collection using the $natural operator, which returns documents in their natural insertion order. By default, find() returns documents in the order they were inserted into the collection. Syntax db.collection.find().sort({ "$natural": 1 }); Where 1 for ascending (insertion order) and -1 for descending (reverse insertion order). Sample Data Let us create a collection with sample documents ? db.orderDocsDemo.insertMany([ {"UserScore": 87}, {"UserScore": 98}, {"UserScore": 99}, {"UserScore": 67}, {"UserScore": ...

Read More

Achieve Pagination with MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 322 Views

You can achieve pagination with the help of limit() and skip() in MongoDB. The skip() method bypasses a specified number of documents, while limit() restricts the number of documents returned. Syntax db.collection.find().skip(numberOfDocumentsToSkip).limit(documentsPerPage); Sample Data Let us create a collection with sample documents ? db.paginationDemo.insertMany([ {"CustomerName": "Chris", "CustomerAge": 23}, {"CustomerName": "Robert", "CustomerAge": 26}, {"CustomerName": "David", "CustomerAge": 24}, {"CustomerName": "Carol", "CustomerAge": 28}, {"CustomerName": "Bob", "CustomerAge": 29} ]); { ...

Read More
Showing 111–120 of 388 articles
« Prev 1 10 11 12 13 14 39 Next »
Advertisements