Database Articles

Page 86 of 547

Find in a dictionary like structure by value with MongoDB?

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

To find documents in a dictionary-like structure by value in MongoDB, use dot notation to specify the exact path to the nested field you want to search. Syntax db.collection.find({ "parentObject.nestedObject.fieldName": "searchValue" }); Sample Data Let us first create a collection with nested dictionary-like documents : db.findInDictionaryDemo.insertMany([ { "_id": 101, "AllCustomerDetails": { "SomeCustomerDetail1": { ...

Read More

Regex to ignore a specific character in MongoDB?

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

To ignore a specific character in MongoDB, use regular expressions with either the $not operator or character negation patterns. These approaches filter out documents containing the unwanted character. Syntax // Method 1: Using character negation db.collection.find({field: /^[^CHARACTER]*$/}); // Method 2: Using $not operator db.collection.find({field: {$not: /CHARACTER/}}); Sample Data db.regexDemo.insertMany([ {"CustomerId": "Customer#1234", "CustomerName": "Chris"}, {"CustomerId": "Customer5678", "CustomerName": "Robert"}, {"CustomerId": "Customer#777", "CustomerName": "Carol"}, {"CustomerId": "Customer777", "CustomerName": "David"} ]); { "acknowledged": true, ...

Read More

How do you perform an AND query on an array in MongoDB?

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

To perform an AND query on an array in MongoDB, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of order or additional elements. Syntax db.collection.find({ "arrayField": { $all: ["value1", "value2", "value3"] } }); Sample Data db.andQueryDemo.insertMany([ { "StudentName": "Carol Taylor", "FavouriteSubject": ["C", "Java", "MongoDB", "MySQL"] }, { ...

Read More

Check if value exists for a field in a MongoDB document?

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

To check if a value exists for a field in a MongoDB document, you can use the $exists operator with the find() method. This operator checks whether a field exists and optionally whether it contains any values. Syntax db.collection.find({ "fieldName": { $exists: true } }); db.collection.find({ "arrayField.0": { $exists: true } }); Sample Data db.checkIfValueDemo.insertMany([ { "PlayerName": "John Smith", "PlayerScores": [5000, 98595858, 554343] }, { ...

Read More

Variable collection name in MongoDB shell with JavaScript?

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

Yes, you can set a variable collection name in MongoDB shell using JavaScript. This allows you to reference collections through variables instead of typing the full collection name repeatedly. Syntax var variableName = db.collectionName; variableName.find(); Sample Data Let us first create a collection with documents ? db.employeeInformation.insertOne({ "EmployeeName": "John Smith", "EmployeeAge": 24, "EmployeeSalary": 450000 }); { "acknowledged": true, "insertedId": ObjectId("5cc6d06baf8e7a4ca6b2ad97") } Display all documents from the collection ...

Read More

How do I know which MongoDB version is installed using the Command Line?

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

To check which MongoDB version is installed on your system, you can use the command line to query the MongoDB server directly. This is useful for verifying your installation and ensuring compatibility with your applications. Syntax mongo --version Or alternatively: mongod --version Method 1: Using mongo --version Open your command prompt and navigate to the MongoDB bin directory. Then execute the following command ? mongo --version MongoDB shell version v4.0.5 git version: 3739429dd92b92d1b0ab120911a23d50bf03c412 OpenSSL version: OpenSSL 1.1.0i 14 Aug 2018 allocator: tcmalloc modules: none ...

Read More

How to remove key fields in MongoDB?

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

To remove key fields in MongoDB, you can use the $unset operator. This operator deletes specified fields from documents in a collection. Syntax db.collection.updateMany( { query }, { $unset: { fieldName: 1 } } ); Sample Data Let us first create a collection with sample documents ? db.removeKeyFieldsDemo.insertMany([ { "StudentFirstName": "John", "StudentLastName": "Doe", "StudentAge": 23 ...

Read More

Retrieve the position in an Array in MongoDB?

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

To retrieve the position of an element in an array in MongoDB, you can use the map-reduce approach with JavaScript's indexOf() method. This technique allows you to find the index position of any array element. Syntax db.collection.mapReduce( function() { emit(this._id, { "IndexValue": this.arrayField.indexOf("searchValue") }); }, function() {}, { "out": { "inline": 1 }, "query": { "arrayField": "searchValue" ...

Read More

MongoDB multidimensional array projection?

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

For MongoDB multidimensional array projection, you need to use the aggregate framework. This allows you to access and project specific elements from nested arrays using operators like $unwind, $project, $skip, and $limit. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $project: { _id: 0, arrayField: 1 } }, { $unwind: "$arrayField" }, { $skip: index }, { $limit: 1 } ]); Sample Data db.multiDimensionalArrayProjection.insertOne({ "StudentFirstName": "Chris", ...

Read More

How to maintain the top count of array elements in MongoDB?

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

To maintain the top count of array elements in MongoDB, use the aggregation framework with $unwind to flatten arrays, $group to count frequencies, $sort to order by count, and $limit to get the top N elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField", count: { $sum: 1 } } }, { $sort: { count: -1 } }, { $limit: N } ]); Sample Data db.topCountArrayDemo.insertMany([ { "StudentId": 101, "StudentSubject": ...

Read More
Showing 851–860 of 5,468 articles
« Prev 1 84 85 86 87 88 547 Next »
Advertisements