Database Articles

Page 96 of 547

How to remove all documents from a collection except a single document in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 845 Views

To remove all documents from a collection except a single document in MongoDB, use deleteMany() or remove() with the $ne operator to exclude documents that match a specific condition. Syntax db.collection.deleteMany({ field: { $ne: value } }); // OR db.collection.remove({ field: { $ne: value } }); Create Sample Data db.removeAllDocumentsExceptOneDemo.insertMany([ {"StudentName": "Larry", "StudentAge": 21}, {"StudentName": "Mike", "StudentAge": 21, "StudentCountryName": "US"}, {"StudentName": "Chris", "StudentAge": 24, "StudentCountryName": "AUS"} ]); { "acknowledged": true, ...

Read More

How to compare field values in MongoDB?

George John
George John
Updated on 15-Mar-2026 367 Views

To compare field values in MongoDB, you can use the $where operator for JavaScript-based comparisons or the $expr operator (recommended) for aggregation expression comparisons. Both methods allow you to compare fields within the same document. Syntax // Using $where (JavaScript evaluation) db.collection.find({ $where: "this.field1 > this.field2" }); // Using $expr (Aggregation expressions - recommended) db.collection.find({ $expr: { $gt: ["$field1", "$field2"] } }); Sample Data db.comparingFieldDemo.insertMany([ {"Value1": 30, "Value2": 40}, {"Value1": 60, "Value2": 70}, {"Value1": 160, "Value2": 190}, ...

Read More

How to find exact Array Match with values in different order using MongoDB?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 502 Views

To find exact array match with values in different order in MongoDB, use the $all operator combined with $size. The $all operator matches arrays containing all specified elements regardless of order, while $size ensures the exact array length. Syntax db.collection.find({ "arrayField": { "$size": exactLength, "$all": [value1, value2, value3] } }); Create Sample Data db.exactMatchArrayDemo.insertMany([ { "StudentName": "David", ...

Read More

Can I query on a MongoDB index if my query contains the $or operator?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 266 Views

Yes, you can query on MongoDB indexes with the $or operator. MongoDB can effectively use separate indexes for each condition in the $or query, combining results through an OR stage in the execution plan. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 } ]}).explain(); Create Sample Indexes First, create indexes on the fields you'll query ? db.indexOrQueryDemo.createIndex({"First": 1}); { "createdCollectionAutomatically": false, "numIndexesBefore": 2, "numIndexesAfter": 3, ...

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

Get the first element in an array and return using MongoDB Aggregate?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 3K+ Views

To get the first element from an array using MongoDB aggregation, use the $arrayElemAt operator or combine $unwind with $first. The $arrayElemAt method is simpler and more efficient for this task. Syntax db.collection.aggregate([ { $project: { "fieldName": { $arrayElemAt: ["$arrayField", 0] } } } ]); Sample Data db.getFirstElementInArrayDemo.insertMany([ { ...

Read More

How to reduce MongoDB storage space after deleting large amount of data?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 539 Views

To reduce MongoDB storage space after deleting a large amount of data, you need to reclaim the freed disk space since MongoDB doesn't automatically release space after deletions. The database continues to use the same amount of disk space even after documents are removed. Syntax db.runCommand({ "compact": "collectionName" }) Or for the entire database: db.repairDatabase() Method 1: Using compact Command (Recommended) The compact command is the modern approach to reclaim space from a specific collection ? db.runCommand({ "compact": "users" }) { "ok" : 1, "bytesFreed" ...

Read More

Concatenate strings from two fields into a third field in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To concatenate strings from two fields into a third field in MongoDB, use the $concat operator within an aggregation pipeline with $project stage. Syntax db.collection.aggregate([ { $project: { "newFieldName": { $concat: [ "$field1", "delimiter", "$field2" ] } } } ]); Sample Data db.concatenateStringsDemo.insertMany([ {"StudentFirstName": "John", "StudentLastName": "Doe"}, {"StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentFirstName": "Carol", "StudentLastName": "Taylor"}, {"StudentFirstName": "David", "StudentLastName": "Miller"}, {"StudentFirstName": "James", "StudentLastName": "Williams"} ]); { ...

Read More

Can we remove _id from MongoDB query result?

George John
George John
Updated on 15-Mar-2026 6K+ Views

To remove _id from MongoDB query result, you need to set 0 for the _id field in the projection parameter. This excludes the _id field from the returned documents. Syntax db.collectionName.find({}, {_id: 0}); Sample Data Let us create a collection with sample documents ? db.removeIdDemo.insertMany([ {"UserName": "John", "UserAge": 23}, {"UserName": "Mike", "UserAge": 27}, {"UserName": "Sam", "UserAge": 34}, {"UserName": "Carol", "UserAge": 29} ]); { "acknowledged": true, ...

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
Showing 951–960 of 5,468 articles
« Prev 1 94 95 96 97 98 547 Next »
Advertisements