Database Articles

Page 80 of 547

How to check if a field in MongoDB is [] or {}?

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

To check if a field in MongoDB is an empty array [] or an empty object {}, you can use the $size operator for arrays and $eq operator for objects to identify these specific empty states. Syntax // Check for empty object {} db.collection.find({ "fieldName": {} }); // Check for empty array [] db.collection.find({ "fieldName": { $size: 0 } }); // Check for both empty array or empty object db.collection.find({ $or: [ { "fieldName": {} }, ...

Read More

How to get all collections where collection name like '%2015%'?

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

To get all collections where the collection name contains a specific pattern like '%2015%', you can use MongoDB's getCollectionNames() method combined with JavaScript's filter() function and regular expressions. Creating Sample Collections Let us first create some collections that contain year numbers like 2015, 2019, etc ? > use web; switched to db web > db.createCollection("2015-myCollection"); { "ok" : 1 } > db.createCollection("2019-employeeCollection"); { "ok" : 1 } > db.createCollection("2015-yourCollection"); { "ok" : 1 } Now you can display all the collections with the help of SHOW command ? > show collections; ...

Read More

MongoDB query to fetch elements between a range excluding both the numbers used to set range?

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

To fetch elements between a range in MongoDB while excluding both boundary numbers, use the $gt (greater than) and $lt (less than) operators together in your query. Syntax db.collectionName.find({ fieldName: { $gt: lowerBound, $lt: upperBound } }); For including both boundary numbers, use $gte and $lte operators ? db.collectionName.find({ fieldName: { $gte: lowerBound, $lte: upperBound } }); Sample Data db.returnEverythingBetween50And60.insertMany([ {"Amount": 55}, {"Amount": 45}, {"Amount": 50}, ...

Read More

Can MongoDB return result of increment?

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

Yes, MongoDB can return the result of an increment operation using the findAndModify() method. This allows you to atomically increment a value and retrieve the updated document in a single operation. Syntax db.collection.findAndModify({ query: { /* match criteria */ }, update: { $inc: { "field": incrementValue } }, new: true }); Sample Data Let us first create a collection with a sample document: db.returnResultOfIncementDemo.insertOne({"PlayerScore": 98}); { "acknowledged": true, "insertedId": ...

Read More

Get the size of all the documents in a MongoDB query?

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

To get the size of all the documents in a MongoDB query, you need to loop through the documents and calculate their cumulative BSON size using Object.bsonsize() method. Syntax var cursor = db.collection.find(); var totalSize = 0; cursor.forEach(function(doc) { totalSize += Object.bsonsize(doc); }); print(totalSize); Sample Data db.sizeOfAllDocumentsDemo.insertMany([ { "StudentFirstName": "John", "StudentSubject": ["MongoDB", "Java"] }, { ...

Read More

How to insert an 8-byte integer into MongoDB through JavaScript shell?

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

To insert an 8-byte integer into MongoDB through JavaScript shell, use the NumberLong() constructor to create a 64-bit integer value. This ensures proper storage of large integer values that exceed JavaScript's standard number precision. Syntax anyVariableName = {"yourFieldName": new NumberLong("yourValue")}; Example Create a variable with an 8-byte integer ? userDetail = {"userId": new NumberLong("98686869")}; { "userId" : NumberLong(98686869) } Display Variable Value Display the variable using the variable name ? userDetail { "userId" : NumberLong(98686869) } Display the variable ...

Read More

How to access subdocument value when the key is a number in MongoDB?

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

To access subdocument values when the key is a number in MongoDB, use bracket notation with quotes around the numeric key. This is necessary because numeric keys are stored as strings in MongoDB subdocuments. Syntax db.collection.findOne().fieldName["numericKey"] db.collection.find({"fieldName.numericKey.subfield": value}) Sample Data db.accessSubDocumentDemo.insertOne({ "Details": { "1": { "StudentLowerScore": "33", "StudentHighScore": "55" ...

Read More

How to pull an array element (which is a document) in MongoDB?

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

To pull (remove) an array element that is a document in MongoDB, use the $pull operator with specific field conditions to match and remove the target document from the array. Syntax db.collection.update( { query }, { $pull: { "arrayField": { "field": "value" } } } ); Sample Data db.pullAnArrayElementDemo.insertOne({ "StudentDetails": [ { "StudentFirstName": "Chris", ...

Read More

Conditional $first in MongoDB aggregation ignoring NULL?

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

To get the first non-NULL value in MongoDB aggregation, use the $match operator to filter out NULL values before applying $first in the $group stage. Syntax db.collection.aggregate([ { $match: { "fieldName": { $ne: null } } }, { $group: { "_id": "$groupField", "result": { $first: "$fieldName" } }} ]); Sample Data db.conditionalFirstDemo.insertMany([ {_id:100, "StudentName":"Chris", "StudentSubject":null}, ...

Read More

How to create a collection correctly in MongoDB to avoid "ReferenceError: Not defined" error?

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

To create a collection correctly in MongoDB and avoid "ReferenceError: Not defined" errors, you must use the proper MongoDB database object syntax with createCollection() method. Syntax db.createCollection("collectionName"); Example: Creating a Collection Let us create a collection called "employeeInformation" in the "sample" database ? use sample; db.createCollection("employeeInformation"); switched to db sample { "ok" : 1 } Verify Collection Creation Display all collections in the current database to confirm the collection was created ? db.getCollectionNames(); [ "arraySizeErrorDemo", "atleastOneMatchDemo", ...

Read More
Showing 791–800 of 5,468 articles
« Prev 1 78 79 80 81 82 547 Next »
Advertisements