Database Articles

Page 82 of 547

Get at least one match in list querying with MongoDB?

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

To get at least one match in list querying with MongoDB, use the $in operator. This operator returns documents where the array field contains at least one of the specified values. Syntax db.collection.find({ "arrayField": { "$in": ["value1", "value2", "value3"] } }); Sample Data Let us create a collection with sample documents − db.atleastOneMatchDemo.insertMany([ {"StudentFavouriteSubject": ["MySQL", "MongoDB"]}, {"StudentFavouriteSubject": ["Java", "C", "MongoDB"]}, {"StudentFavouriteSubject": ["Python", "C++", "SQL Server"]}, {"StudentFavouriteSubject": ["Ruby", "Javascript", "C#", "MySQL"]} ]); ...

Read More

How to filter array elements in MongoDB?

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

To filter array elements in MongoDB, you can use the $setIntersection operator within the aggregation framework. This operator returns elements that exist in both the original array and your filter criteria array. Syntax db.collection.aggregate([ { $project: { fieldName: { $setIntersection: ["$arrayField", [filterValues]] } }} ]); Sample Data db.filterArrayElementsDemo.insertOne({ "Scores": [10, 45, 67, 78, ...

Read More

What is the MongoDB Capped Collection maximum allowable size?

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

MongoDB capped collections can be set to any size you specify using the size parameter. There is no fixed maximum limit − the size is constrained only by your available disk space and system resources. Syntax db.createCollection('collectionName', { capped: true, size: sizeInBytes }); Example Create a capped collection with a large size limit ? db.createCollection('cappedCollectionMaximumSize', { capped: true, size: 1948475757574646 }); { "ok" : 1 } Verify Collection Properties Check ...

Read More

How to set limit to $inc in MongoDB?

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

To set a limit to $inc in MongoDB, use a query condition with comparison operators like $lt to only increment values that meet specific criteria. This prevents incrementing values beyond a desired threshold. Syntax db.collection.updateMany( { fieldName: { $lt: limitValue } }, { $inc: { fieldName: incrementValue } } ); Create Sample Data db.limitIncrementDemo.insertMany([ { "StudentId": 101, "StudentScore": 95 }, { "StudentId": 102, "StudentScore": 55 }, { "StudentId": 103, "StudentScore": 67 }, ...

Read More

Retrieve values from nested JSON array in MongoDB?

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

To retrieve values from nested JSON array in MongoDB, use dot notation to navigate through the nested structure. This allows you to query specific fields within arrays and embedded documents. Syntax db.collectionName.find({ "outerField.arrayField.nestedField": "value" }); Sample Data Let us create a collection with nested JSON array documents ? db.nestedJSONArrayDemo.insertMany([ { "ClientDetails": { "ClientPersonalDetails": [ ...

Read More

Find and replace NumberLong type field in MongoDB?

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

To find and replace NumberLong type fields in MongoDB, use the $set operator with update() or updateMany(). This allows you to match documents containing specific NumberLong values and replace them with new NumberLong values. Syntax db.collection.update( { "fieldName": NumberLong(oldValue) }, { $set: { "fieldName": NumberLong(newValue) } }, { multi: true } ); Sample Data db.findAndReplaceDemo.insertMany([ { "UserId": NumberLong(101) }, { "UserId": NumberLong(110) }, { "UserId": NumberLong(101) }, ...

Read More

Find objects created in last week in MongoDB?

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

To find objects created in the last week in MongoDB, use the $gte operator with a date calculation that subtracts 7 days from the current date. This filters documents with dates greater than or equal to one week ago. Syntax db.collection.find({ "dateField": { $gte: new Date(new Date() - 7 * 60 * 60 * 24 * 1000) } }); Sample Data Let us first create a collection with documents to demonstrate the query ? db.findObjectInLastWeekDemo.insertMany([ ...

Read More

Find the MongoDB document from sub array?

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

To find documents from a sub array in MongoDB, use dot notation to access nested array elements. This allows you to query specific fields within arrays that are embedded in your documents. Syntax db.collection.find({ "parentField.arrayField.subField": "value" }); Sample Data Let us create sample documents with employee appraisal data ? db.findDocumentDemo.insertMany([ { "EmployeeDetails": { "EmployeeAppraisalTime": [ ...

Read More

Increment a field in MongoDB document which is embedded?

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

To increment a field in a MongoDB document that is embedded within nested objects, use the $inc operator with dot notation to specify the path to the embedded field. Syntax db.collection.update( { "query": "condition" }, { $inc: { "parent.child.field": incrementValue } } ); Sample Data Let's create a document with embedded StudentScores inside StudentDetails: db.embeddedValueIncrementDemo.insertOne({ "StudentDetails": { "StudentScores": { "StudentMathScore": ...

Read More

Selecting database inside the JS in MongoDB?

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

To select a database inside JavaScript in MongoDB, use the getSiblingDB() method with the var keyword to create a reference to another database from the current connection. Syntax var variableName = db.getSiblingDB('databaseName'); Example Let's select a database named 'sample' using getSiblingDB() ? selectedDatabase = db.getSiblingDB('sample'); sample Working with the Selected Database Now insert some documents into the selected database. Let's use a collection named 'selectDatabaseDemo' ? selectedDatabase.selectDatabaseDemo.insertMany([ {"ClientName": "John Smith", "ClientAge": 23}, {"ClientName": "Carol Taylor", "ClientAge": ...

Read More
Showing 811–820 of 5,468 articles
« Prev 1 80 81 82 83 84 547 Next »
Advertisements