Smita Kapse

Smita Kapse

388 Articles Published

Articles by Smita Kapse

Page 9 of 39

How to implement MongoDB $or operator

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

The $or operator in MongoDB performs a logical OR operation on an array of expressions and returns documents that match at least one of the specified conditions. Syntax db.collection.find({ $or: [ { "field1": value1 }, { "field2": value2 } ] }); Create Sample Data db.orOperatorDemo.insertMany([ { "StudentNames": ["John", "Carol", "Sam"] }, { "StudentNames": ["Robert", "Chris", "David"] }, ...

Read More

How to find documents with exactly the same array entries as in a MongoDB query?

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

To find documents with exactly the same array entries as specified in a MongoDB query, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of order. Syntax db.collection.find({ "arrayField": { "$all": ["element1", "element2", "element3"] } }); Sample Data db.findDocumentExactlySameInArrayDemo.insertMany([ {"TechnicalSubjects": ["C++", "Java", "MongoDB"]}, {"TechnicalSubjects": ["MySQL", "Java", "MongoDB"]}, {"TechnicalSubjects": ["C#", "Python", "MongoDB"]}, {"TechnicalSubjects": ["MySQL", "C", "MongoDB"]} ]); { ...

Read More

What should be used to implement MySQL LIKE statement in MongoDB?

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

To implement MySQL LIKE statement functionality in MongoDB, use the $regex operator. This provides pattern matching capabilities similar to SQL's LIKE operator for text searches. Syntax db.collection.find({ "fieldName": { $regex: "pattern" } }); // Alternative syntax db.collection.find({ "fieldName": /pattern/ }); Sample Data db.likeInMongoDBDemo.insertMany([ { "Name": "Sam" }, { "Name": "John" }, { "Name": "Scott" }, { "Name": "Sean" }, { "Name": "Samuel" } ]); { ...

Read More

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

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 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

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

Selecting only a single field from MongoDB?

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

To select only a single field from MongoDB, use the projection parameter in the find() method. Set the desired field to 1 to include it, and set _id to 0 to exclude the default ID field. Syntax db.collection.find( { query }, { fieldName: 1, _id: 0 } ); Sample Data db.selectingASingleFieldDemo.insertMany([ {"StudentFirstName": "John", "StudentAge": 23, "StudentCountryName": "US"}, {"StudentFirstName": "Carol", "StudentAge": 21, "StudentCountryName": "UK"}, {"StudentFirstName": "David", "StudentAge": 24, "StudentCountryName": "AUS"}, ...

Read More

Want to update inner field in a MongoDB

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

To update an inner field in MongoDB, use the $set operator with dot notation to target the nested field within a document. This allows you to modify specific fields inside embedded documents without affecting other fields. Syntax db.collectionName.update( {"_id": ObjectId}, {$set: {"outerField.innerField": newValue}} ); Sample Data Let us create a collection with a document containing nested fields ? db.updateDocumentDemo.insertOne({ "StudentDetails": { "StudentFirstName": "Adam", "StudentLastName": ...

Read More

How to find MongoDB documents in a collection with a filter on multiple combined fields?

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

To find MongoDB documents with filters on multiple combined fields, use the $or or $and operators within the find() method. These operators allow you to combine multiple conditions across different fields. Syntax // OR operator - matches documents that satisfy ANY condition db.collection.find({ $or: [ { "field1": condition1 }, { "field2": condition2 } ] }); // AND operator - matches documents that satisfy ALL conditions db.collection.find({ ...

Read More
Showing 81–90 of 388 articles
« Prev 1 7 8 9 10 11 39 Next »
Advertisements