Arjun Thakur

Arjun Thakur

749 Articles Published

Articles by Arjun Thakur

Page 29 of 75

Update only specific fields in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 874 Views

To update only specific fields in MongoDB, use the $set operator. This operator modifies the value of a field without affecting other fields in the document. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Sample Data db.updateOnlySpecificFieldDemo.insertMany([ { "EmployeeName": "John", "EmployeeCountryName": "UK" }, { "EmployeeName": "Larry", "EmployeeCountryName": "US" }, { "EmployeeName": "David", "EmployeeCountryName": "AUS" } ]); { "acknowledged": true, ...

Read More

Find the document by field name with a specific value in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 689 Views

To find documents by field name with a specific value in MongoDB, you can use the $exists operator to check if a field exists, or use dot notation to match specific field values in nested documents. Syntax // Check if field exists db.collection.find({"fieldName": {$exists: true}}) // Find by specific value in nested field db.collection.find({"parent.child": "specificValue"}) Sample Data db.findByFieldName.insertMany([ { "Client": { "ClientDetails": { ...

Read More

How to improve querying field in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 187 Views

To improve querying performance in MongoDB, you need to use indexes. Indexes create efficient data structures that allow MongoDB to quickly locate documents without scanning the entire collection. Syntax db.collection.createIndex({ "fieldName": 1 }); db.collection.find({ "fieldName": "searchValue" }); Sample Data Let us create a collection with documents ? db.improveQueryDemo.insertOne({ "PlayerDetails": [ {"PlayerName": "John", "PlayerGameScore": 5690}, {"PlayerName": "Carol", "PlayerGameScore": 2690} ] }); { ...

Read More

How can I use MongoDB to find all documents which have a field, regardless of the value of that field?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 179 Views

To use MongoDB to find all documents which have a field, regardless of the value of that field, use the $exists operator. This operator returns documents where the specified field exists, even if its value is null or empty. Syntax db.collectionName.find({ fieldName: { $exists: true } }); Sample Data Let us create a collection with sample documents ? db.students.insertMany([ { "StudentName": "John", "StudentAge": null }, { "StudentName": "Larry", "StudentAge": null }, { "StudentName": "Chris", "StudentAge": "" ...

Read More

How to insert a document with date in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 3K+ Views

To insert a document with date in MongoDB, use the new Date() constructor to create date values. MongoDB stores dates as ISODate objects in UTC format. Syntax db.collection.insertOne({ "fieldName": new Date("YYYY-MM-DD") }); Example Let us create a collection with documents containing date fields ? db.insertDocumentWithDateDemo.insertMany([ { "UserName": "Larry", "UserMessage": "Hi", "UserMessagePostDate": new Date("2012-09-24") }, ...

Read More

How to loop through collections with a cursor in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 2K+ Views

In MongoDB, you can loop through collections with a cursor using the while loop with hasNext() and next() methods. A cursor is returned by the find() method and allows you to iterate through documents one by one. Syntax var cursor = db.collectionName.find(); while(cursor.hasNext()) { var document = cursor.next(); printjson(document); } Create Sample Data Let us create a collection with student documents ? db.loopThroughCollectionDemo.insertMany([ {"StudentName": "John", "StudentAge": 23}, {"StudentName": "Larry", "StudentAge": 21}, {"StudentName": ...

Read More

How to retrieve the documents whose values end with a particular character in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 2K+ Views

To retrieve documents whose field values end with a particular character in MongoDB, use the $regex operator with the dollar sign ($) anchor to match the end of the string. Syntax db.collection.find({ fieldName: { $regex: "character$" } }); The $ symbol ensures the pattern matches only at the end of the string. Sample Data Let us create a collection with sample documents ? db.students.insertMany([ { "StudentName": "Adam", "StudentAge": 25, "StudentCountryName": "LAOS" }, { "StudentName": "Sam", "StudentAge": 24, "StudentCountryName": "ANGOLA" ...

Read More

How to change the password in MongoDB for existing user?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 1K+ Views

To change the password in MongoDB for an existing user, use the changeUserPassword() method. This operation requires administrative privileges and must be performed on the database where the user was created. Syntax db.changeUserPassword("username", "newPassword"); Step 1: Switch to Admin Database First, switch to the admin database where user management operations are performed ? use admin switched to db admin Step 2: Display Existing Users Check the current users in the database ? db.getUsers(); [ { ...

Read More

Get Distinct Values with Sorted Data in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 1K+ Views

To get distinct values with sorted data in MongoDB, use the distinct() method followed by sort(). The distinct() method returns unique values from a specified field, and sort() arranges them in alphabetical or numerical order. Syntax db.collection.distinct("fieldName").sort() Sample Data db.getDistinctWithSortedDataDemo.insertMany([ {"StudentId": 10, "StudentName": "John", "StudentAge": 23}, {"StudentId": 20, "StudentName": "Carol", "StudentAge": 21}, {"StudentId": 10, "StudentName": "John", "StudentAge": 23}, {"StudentId": 30, "StudentName": "Chris", "StudentAge": 22}, {"StudentId": 20, "StudentName": "Carol", "StudentAge": 21}, ...

Read More

Pull and add to set at the same time with MongoDB? Is it Possible?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 902 Views

Yes, you can use pull and add operations simultaneously in MongoDB using $addToSet and $pull operators with bulk operations. This allows you to modify arrays in multiple ways within a single database round trip. Syntax var bulkOp = db.collection.initializeOrderedBulkOp(); bulkOp.find({condition1}).updateOne({$addToSet: {arrayField: newValue}}); bulkOp.find({condition2}).updateOne({$pull: {arrayField: valueToRemove}}); bulkOp.execute(); Sample Data db.pullAndAddToSetDemo.insertOne({StudentScores: [78, 89, 90]}); { "acknowledged": true, "insertedId": ObjectId("5c9a797e15e86fd1496b38af") } Let's view the initial document ? db.pullAndAddToSetDemo.find().pretty(); { "_id": ObjectId("5c9a797e15e86fd1496b38af"), ...

Read More
Showing 281–290 of 749 articles
« Prev 1 27 28 29 30 31 75 Next »
Advertisements