Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Smita Kapse
Page 12 of 39
How to do alphanumeric sort in MongoDB?
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 MoreReverse array field in MongoDB?
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 MoreHow to increment a field in MongoDB?
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 MoreRemove and update the existing record in MongoDB?
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 MoreHow do I know which MongoDB version is installed using the Command Line?
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 MoreRetrieve the position in an Array in MongoDB?
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 MoreHow to create a user in MongoDB v3?
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 MoreUse result from MongoDB in shell script?
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 MoreHow does MongoDB order their docs in one collection?
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 MoreAchieve Pagination with MongoDB?
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