MongoDB Articles

Page 89 of 111

How to query an Array[String] for a Regular Expression match?

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

To query an array of strings for a regular expression match in MongoDB, use the regular expression pattern directly in the find query. MongoDB automatically searches through array elements and matches any string that satisfies the regex pattern. Syntax db.collection.find({ arrayField: /regexPattern/ }) Create Sample Data db.queryArrayDemo.insertMany([ { "StudentFullName": [ "Carol Taylor", "Caroline Williams", ...

Read More

How to find and modify a value in a nested array?

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

To find and modify a value in a nested array, use the $elemMatch operator to locate the array element and the $ positional operator with $set to update the specific field value. Syntax db.collection.update( { arrayName: { $elemMatch: { field: "matchValue" } } }, { $set: { "arrayName.$.targetField": "newValue" } } ); Sample Data db.findAndModifyAValueInNestedArrayDemo.insertOne({ "CompanyName": "Amazon", "DeveloperDetails": [ { ...

Read More

Get Absolute value with MongoDB aggregation framework?

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

To get absolute values in MongoDB aggregation framework, use the $abs operator within the $project stage. The $abs operator returns the absolute value of a number, converting negative numbers to positive while keeping positive numbers unchanged. Syntax db.collection.aggregate([ { $project: { fieldName: { $abs: "$numberField" } } } ]); Sample Data Let us create a collection with positive, ...

Read More

How to query on top N rows in MongoDB?

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

To query the top N rows in MongoDB, use the aggregation framework with $sort and $limit operators. This approach allows you to sort documents by any field and retrieve only the specified number of top results. Syntax db.collection.aggregate([ { $sort: { fieldName: 1 } }, // 1 for ascending, -1 for descending { $limit: N } // N = number of documents to return ]); Sample Data db.topNRowsDemo.insertMany([ ...

Read More

How do we print a variable at the MongoDB command prompt?

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

To print a variable at the MongoDB command prompt, you can either reference the variable name directly or use the print() function. Both methods display the variable's value in the shell. Syntax // Declaring and initializing a variable var variableName = value; // Method 1: Direct variable reference variableName; // Method 2: Using print() function print(variableName); Example 1: Integer Variable Declare and initialize an integer variable ? var myIntegerValue = 20; Print the variable using direct reference ? myIntegerValue; 20 Print ...

Read More

How do I find all documents with a field that is NaN in MongoDB?

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

To find all documents with a field that is NaN in MongoDB, use the $eq operator or direct comparison with NaN value in your query. Syntax db.collection.find({ fieldName: NaN }); Or using the $eq operator: db.collection.find({ fieldName: { $eq: NaN } }); Create Sample Data First, let's create a collection with documents containing various numerical values including NaN ? db.nanDemo.insertMany([ { "Score": 0/0 }, // NaN { "Score": 10/5 }, ...

Read More

How can I use $elemMatch on first level array in MongoDB?

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

You can use $in operator instead of $elemMatch on first level array. For simple value matching in arrays, $in provides better performance and cleaner syntax. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2"] } }); Sample Data Let us create a collection with documents containing first−level arrays ? db.firstLevelArrayDemo.insertMany([ { "StudentName": "Chris", "StudentTechnicalSkills": ["MongoDB", "MySQL", "SQL Server"] }, { ...

Read More

Retrieving the first document in a MongoDB collection?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 3K+ Views

To retrieve the first document in a MongoDB collection, use the findOne() method. This returns the first document from the collection in natural insertion order. Syntax db.collectionName.findOne(); // Store in variable and display var result = db.collectionName.findOne(); result; Sample Data db.retrieveFirstDocumentDemo.insertMany([ {"ClientName": "Robert", "ClientAge": 23}, {"ClientName": "Chris", "ClientAge": 26}, {"ClientName": "Larry", "ClientAge": 29}, {"ClientName": "David", "ClientAge": 39} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Using find() to search for nested keys in MongoDB?

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

To search for nested keys in MongoDB, use dot notation to specify the path to the nested field. This allows you to query documents based on values within embedded objects. Syntax db.collectionName.find({"outerField.innerField": "value"}); Sample Data Let us create a collection with nested documents ? db.searchForNestedKeysDemo.insertMany([ { "ClientName": "Larry", "ClientAge": 28, "ClientExtraDetails": { ...

Read More

How to prevent MongoDB from returning the object ID while finding a document?

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

To prevent MongoDB from returning the Object ID while finding a document, you need to set _id to 0 in the projection parameter of the find() method. This excludes the _id field from the query results. Syntax db.collection.find( { query }, { "_id": 0, "field1": 1, "field2": 1 } ); Sample Data Let us first create a collection with a sample document ? db.preventObjectIdDemo.insertOne({ "StudentName": "Chris", "StudentDetails": [ ...

Read More
Showing 881–890 of 1,106 articles
« Prev 1 87 88 89 90 91 111 Next »
Advertisements