MongoDB Articles

Page 92 of 111

How to find a record by _id in MongoDB?

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

To find a record by _id in MongoDB, use the find() method with the ObjectId as the query criteria. Every document in MongoDB has a unique _id field that serves as the primary key. Syntax db.collectionName.find({"_id": ObjectId("your_object_id")}); Sample Data Let us create a collection with sample customer documents ? db.findRecordByIdDemo.insertMany([ {"CustomerName": "Larry", "CustomerAge": 26}, {"CustomerName": "Bob", "CustomerAge": 20}, {"CustomerName": "Carol", "CustomerAge": 22}, {"CustomerName": "David", "CustomerAge": 24} ]); { "acknowledged": ...

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

MongoDB equivalent of SELECT field AS `anothername`?

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

In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB using the $project stage in aggregation pipeline. Syntax db.collectionName.aggregate([ { "$project": { "_id": 0, "aliasName": "$originalFieldName" }} ]); Sample Data Let us first create a collection with documents ? db.selectFieldAsAnotherNameDemo.insertMany([ {"Name": "Larry"}, {"Name": "Robert"}, ...

Read More

How to use $slice operator to get last element of array in MongoDB?

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

To get the last element of an array in MongoDB, use the $slice operator with -1 in the projection. This returns only the last element of the specified array field. Syntax db.collectionName.find( {}, { arrayFieldName: { $slice: -1 } } ); Sample Data Let us create a collection with sample student documents ? db.getLastElementOfArrayDemo.insertMany([ { "StudentName": "James", "StudentMathScore": [78, 68, 98] ...

Read More

Can I retrieve multiple documents from MongoDB by id?

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

Yes, you can retrieve multiple documents from MongoDB by id using the $in operator. This operator allows you to specify multiple values and returns documents where the field matches any of those values. Syntax db.collection.find({ _id: { $in: [id1, id2, id3, ...] } }); Sample Data Let us create a collection with sample documents ? db.retrieveMultipleDocsByIdDemo.insertMany([ { "_id": 10, "CustomerName": "John" }, { "_id": 14, "CustomerName": "Chris" }, { "_id": 20, "CustomerName": "Robert" }, ...

Read More

Insert to specific index for MongoDB array?

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

To insert an element at a specific index in a MongoDB array, use the $push operator combined with $each and $position modifiers. This allows precise control over where new elements are inserted in the array. Syntax db.collection.update( { _id: ObjectId("document_id") }, { $push: { arrayField: { $each: [ "newElement" ], ...

Read More

MongoDB query which represents not equal to null or empty?

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

To set a query for not equal to null or empty, use the $nin operator. This operator selects documents where the field value is not in the specified array of values, effectively filtering out null and empty string values. Syntax db.yourCollectionName.find({yourFieldName: {$nin: [null, ""]}}); Sample Data db.notEqualToNullOrEmptyDemo.insertMany([ {"UserName": "Larry", "UserAge": 24}, {"UserName": "", "UserAge": 29}, {"UserName": "Sam", "UserAge": 32}, {"UserName": null, "UserAge": 27}, {"UserName": "Robert", "UserAge": 26}, {"UserName": "", ...

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

Removing all collections whose name matches a string in MongoDB

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

To remove all collections whose name matches a string in MongoDB, use a for loop to iterate over all collections, check for the string pattern using indexOf(), and apply the drop() method to matching collections. Syntax var allCollectionNames = db.getCollectionNames(); for(var i = 0; i < allCollectionNames.length; i++){ var colName = allCollectionNames[i]; if(colName.indexOf('searchString') == 0){ db[colName].drop(); } } Sample Data Let's say we are using the database "sample" with the following collections ? ...

Read More

How to delete multiple ids in MongoDB?

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

To delete multiple documents by their IDs in MongoDB, use the $in operator with the remove() or deleteMany() method. This allows you to specify multiple ObjectIds in a single query. Syntax db.collectionName.remove({ _id: { $in: [ObjectId("id1"), ObjectId("id2"), ObjectId("id3")] } }); Or using the modern deleteMany() method: db.collectionName.deleteMany({ _id: { $in: [ObjectId("id1"), ObjectId("id2"), ObjectId("id3")] } }); Sample Data db.deleteMultipleIdsDemo.insertMany([ {"ClientName": "Chris", "ClientAge": 26}, {"ClientName": "Robert", "ClientAge": 28}, ...

Read More
Showing 911–920 of 1,106 articles
« Prev 1 90 91 92 93 94 111 Next »
Advertisements