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 82 of 547
Get at least one match in list querying with MongoDB?
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 MoreHow to filter array elements in MongoDB?
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 MoreWhat is the MongoDB Capped Collection maximum allowable size?
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 MoreHow to set limit to $inc in MongoDB?
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 MoreRetrieve values from nested JSON array in MongoDB?
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 MoreFind and replace NumberLong type field in MongoDB?
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 MoreFind objects created in last week in MongoDB?
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 MoreFind the MongoDB document from sub array?
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 MoreIncrement a field in MongoDB document which is embedded?
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 MoreSelecting database inside the JS in MongoDB?
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