MongoDB Articles

Page 77 of 111

Find all documents that have two specific id's in an array of objects in MongoDB?

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

To find all documents that have two specific id's in an array of objects in MongoDB, use the $and operator with dot notation to match multiple values within the same array field. Syntax db.collection.find({ $and: [ { "arrayField.property": value1 }, { "arrayField.property": value2 } ] }); Sample Data db.twoSpecificIdsDemo.insertMany([ { PlayerId: 1, ...

Read More

MongoDB pull with positional operator?

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

The $pull operator combined with the positional operator ($) in MongoDB allows you to remove specific values from arrays within nested documents. Use $elemMatch to identify the target document and $ to reference the matched array element. Syntax db.collection.update( { "arrayField": { "$elemMatch": { "field": "matchValue" ...

Read More

Search a sub-field on MongoDB?

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

To search a sub-field in MongoDB, use dot notation with the field path enclosed in double quotes. This allows you to query nested fields within embedded documents. Syntax db.collection.find({"parentField.subField": "value"}); Sample Data db.searchSubFieldDemo.insertMany([ { "UserDetails": { "UserEmailId": "John123@gmail.com", "UserAge": 21 } }, ...

Read More

How to define aliases in the MongoDB Shell?

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

To define aliases in the MongoDB shell, you can create shortcuts for frequently used functions or expressions using Object.defineProperty(). This allows you to create reusable commands with custom names. Syntax Object.defineProperty(this, 'yourFunctionName', { get: function() { // your statements here return someValue; }, enumerable: true, configurable: true }); To assign the alias to a variable: var anyAliasName = yourFunctionName; Example Let's create an alias called displayMessageDemo ...

Read More

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

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 219 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 178 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 401 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 276 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
Showing 761–770 of 1,106 articles
« Prev 1 75 76 77 78 79 111 Next »
Advertisements