Database Articles

Page 72 of 547

MongoDB query to find data from an array inside an object?

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

To find data from an array inside an object in MongoDB, use dot notation to navigate through the nested object structure and target the specific array field. Syntax db.collection.find({"objectField.arrayField": "searchValue"}); Sample Data Let us first create a collection with documents ? db.findDataDemo.insertMany([ { "CustomerName": "John", "CustomerDetails": { "CountryName": ["AUS"], ...

Read More

Projection result as an array of selected items in MongoDB?

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

Use the distinct() method to project results as an array of selected items in MongoDB. This method finds distinct values for a specified field across a collection and returns them in an array format. Syntax db.collection.distinct("field", query) Sample Data Let us first create a collection with documents ? db.projectionListDemo.insertMany([ {"_id": "1", "Subject": ["MongoDB", "MySQL", "Java"]}, {"_id": "2", "Subject": ["MongoDB", "C", "C++"]}, {"_id": "3", "Subject": ["Java", "Python"]} ]); { "acknowledged": true, ...

Read More

Removing an array element from MongoDB collection using update() and $pull

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

To remove a specific element from an array in MongoDB, use the $pull operator with the update() method. This operator removes all instances of a value that match the specified condition from an array field. Syntax db.collection.update( { query }, { $pull: { "arrayField": "valueToRemove" } } ); Create Sample Data Let us first create a collection with documents ? db.removingAnArrayElementDemo.insertOne({ "UserMessage": ["Hi", "Hello", "Bye"] }); { "acknowledged": true, ...

Read More

Write an equality in MongoDB without using $eq operator

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

In MongoDB, you can write equality queries without using the $eq operator by using the implicit equality syntax. This involves directly specifying the field name and value in the query document. Syntax db.collection.find({"fieldName": "value"}); Sample Data Let us first create a collection with documents ? db.operatorDemo.insertMany([ {"StudentSubject": ["MongoDB", "MySQL", "Java"]}, {"StudentSubject": ["Java", "C", "C++"]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cef94eaef71edecf6a1f6a2"), ...

Read More

MongoDB query to find a value from JSON like data?

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

To find a value from JSON-like data in MongoDB, use the find() method along with dot notation to access nested fields within embedded documents and arrays. Syntax db.collection.find({"field.nestedField": "value"}) Sample Data db.findValueFromJsonDemo.insertOne({ "UserDetails": [{ "_id": ObjectId(), "UserName": "Carol", "UserMessage": "Hi" }], "UserFriendsName": ["John", "Sam"] }); { "acknowledged": true, ...

Read More

MongoDB Limit fields and slice projection together?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 303 Views

To limit fields and use slice projection together in MongoDB, use the $slice operator within the projection parameter of the find() method. This allows you to control which fields are returned while simultaneously limiting array elements using slice. Syntax db.collection.find( { query }, { "field1": 0, "field2": 0, "arrayField": { "$slice": [startIndex, count] } } ); Sample Data ...

Read More

How to add a sub-document to sub-document array in MongoDB?

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

To add a sub-document to a sub-document array in MongoDB, use the $push operator. This operator appends a new document to an existing array field within your MongoDB collection. Syntax db.collection.update( { "matchField": "value" }, { "$push": { "arrayField": { newSubDocument } } } ) Sample Data Let us first create a collection with documents ? db.subDocumentToSubDocumentDemo.insertOne( { "_id": 101, "StudentName": "Larry", ...

Read More

Check if a list is not empty in MongoDB?

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

To check if a list is not empty in MongoDB, use the $not operator combined with $size to exclude arrays with zero elements. This returns documents where the array field contains at least one element. Syntax db.collection.find({ "arrayField": { "$not": { "$size": 0 } } }); Sample Data db.checkIfListIsNotEmptyDemo.insertMany([ { "UserFriendGroup": ["John", "David"] }, { "UserFriendGroup": ["Carol"] }, { "UserFriendGroup": [] }, { "UserFriendGroup": [null] }, { "UserFriendGroup": [] ...

Read More

Find data for specific date in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To find data for a specific date in MongoDB, use the $gte and $lt operators to create a date range. These operators help filter documents where the date field falls within the specified range. Syntax db.collection.find({ "dateField": { "$gte": new Date("YYYY-MM-DD"), "$lt": new Date("YYYY-MM-DD") } }); Sample Data db.findDataByDateDemo.insertMany([ { "UserName": "John", "UserLoginDate": new ISODate("2019-01-31") }, { "UserName": "Larry", "UserLoginDate": new ...

Read More

Efficient way to remove all entries from MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 294 Views

MongoDB provides two main methods to remove all entries from a collection: drop() and remove(). The key difference is that drop() deletes the entire collection including indexes, while remove() only deletes documents but preserves the collection structure and indexes. Syntax // Method 1: Drop entire collection (fastest) db.collection.drop() // Method 2: Remove all documents (preserves indexes) db.collection.remove({}) // Method 3: Delete all documents (modern approach) db.collection.deleteMany({}) Method 1: Using drop() (Recommended for Complete Removal) Let us first create a collection with documents and indexes ? db.dropWorkingDemo.createIndex({"FirstName":1}); db.dropWorkingDemo.insertOne({"FirstName":"John"}); ...

Read More
Showing 711–720 of 5,468 articles
« Prev 1 70 71 72 73 74 547 Next »
Advertisements