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
Database Articles
Page 72 of 547
MongoDB query to find data from an array inside an object?
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 MoreProjection result as an array of selected items in MongoDB?
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 MoreRemoving an array element from MongoDB collection using update() and $pull
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 MoreWrite an equality in MongoDB without using $eq operator
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 MoreMongoDB query to find a value from JSON like data?
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 MoreMongoDB Limit fields and slice projection together?
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 MoreHow to add a sub-document to sub-document array in MongoDB?
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 MoreCheck if a list is not empty in MongoDB?
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 MoreFind data for specific date in MongoDB?
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 MoreEfficient way to remove all entries from MongoDB?
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