Database Articles

Page 77 of 547

Find the count of users who logged in between specific dates with MongoDB

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

To find the count of users who logged in between specific dates in MongoDB, use the count() method with $gte and $lt operators to define the date range filter. Syntax db.collection.count({ "dateField": { "$gte": new Date("start-date"), "$lt": new Date("end-date") } }); Sample Data db.findDataByDateDemo.insertMany([ { "UserName": "John", "UserLoginDate": new ISODate("2019-01-31") }, { "UserName": "Larry", "UserLoginDate": new ISODate("2019-02-01") }, ...

Read More

Calculate the average value in a MongoDB document grouping by null?

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

To calculate the average value across all documents in a MongoDB collection, use the $group operator with _id: null to group all documents together, then apply the $avg accumulator operator. Syntax db.collectionName.aggregate([ { $group: { _id: null, "averageFieldName": { $avg: "$fieldName" } } } ]); Sample Data ...

Read More

Implement MongoDB $concatArrays even when some of the values are null?

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

Use the $ifNull operator with $concatArrays in the aggregation framework to concatenate arrays even when some fields are null or missing. The $ifNull operator replaces null values with an empty array, allowing concatenation to proceed successfully. Syntax db.collection.aggregate([ { $project: { concatenatedField: { $concatArrays: [ { $ifNull: ["$array1", []] }, { $ifNull: ["$array2", []] } ...

Read More

MongoDB query check if value in array property?

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

To check if a value exists in an array property in MongoDB, you can use the $in operator for exact matches. This operator searches for documents where the array field contains any of the specified values. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2"] } }); Sample Data Let's create a collection with sample documents ? db.valueInArrayDemo.insertMany([ { "UserName": "John", "UserMessage": ["Hi", "Hello", "Bye"] }, ...

Read More

How do I push elements to an existing array in MongoDB?

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

To push elements to an existing array in MongoDB, use the $addToSet or $push operators with the update() method. The $addToSet adds elements only if they don't already exist, while $push always adds elements regardless of duplicates. Syntax // Using $addToSet (prevents duplicates) db.collection.update( {query}, { $addToSet: { "arrayField": "newElement" } } ); // Using $push (allows duplicates) db.collection.update( {query}, { $push: { "arrayField": "newElement" } } ); Sample Data Let us create a collection with a ...

Read More

How to print document value in MongoDB shell?

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

To print specific document values in MongoDB shell, use the forEach() method combined with the print() function. This allows you to extract and display individual field values from query results. Syntax db.collection.find(query, projection).forEach(function(document) { print(document.fieldName); }); Sample Data db.printDocumentValueDemo.insertMany([ {"InstructorName": "John Smith"}, {"InstructorName": "Sam Williams"}, {"InstructorName": "David Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cd6804f7924bb85b3f48950"), ...

Read More

How to remove white spaces (leading and trailing) from string value in MongoDB?

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

To remove white spaces (leading and trailing) from string values in MongoDB, you can use the forEach() method combined with JavaScript's trim() function to iterate through documents and update them. Syntax db.collection.find().forEach(function(doc) { doc.fieldName = doc.fieldName.trim(); db.collection.update( { "_id": doc._id }, { "$set": { "fieldName": doc.fieldName } } ); }); Sample Data Let us first create a collection with documents containing white spaces ? ...

Read More

How to add a field with static value to MongoDB find query?

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

To add a field with static value to MongoDB find query, use the $literal operator with the aggregation framework. The $literal operator returns a value without parsing, making it perfect for adding constant values to query results. Syntax db.collection.aggregate([ { $project: { field1: 1, field2: 1, "staticFieldName": { $literal: ...

Read More

How to get tag count in MongoDB query results based on list of names?

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

To get tag count in MongoDB query results based on a list of names, use the $in operator to match documents containing any of the specified names in an array field, then apply count() to get the total number of matching documents. Syntax db.collection.find({"arrayField": {$in: ["value1", "value2"]}}).count(); Sample Data db.tagCountDemo.insertMany([ {"ListOfNames": ["John", "Sam", "Carol"]}, {"ListOfNames": ["Bob", "David", "John"]}, {"ListOfNames": ["Mike", "Robert", "Chris"]}, {"ListOfNames": ["James", "Carol", "Jace"]} ]); { "acknowledged": true, ...

Read More

Get documents expired before today in MongoDB?

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

To get documents expired before today in MongoDB, use the $lte operator with new Date() to find all documents where the date field is less than or equal to the current date. Syntax db.collection.find({ "dateField": { $lte: new Date() } }); Sample Data db.getDocumentsExpiredDemo.insertMany([ { "ArrivalDate": new ISODate("2019-05-11") }, { "ArrivalDate": new ISODate("2019-01-01") }, { "ArrivalDate": new ISODate("2019-05-10") }, { "ArrivalDate": new ISODate("2019-02-01") } ]); { "acknowledged": true, ...

Read More
Showing 761–770 of 5,468 articles
« Prev 1 75 76 77 78 79 547 Next »
Advertisements