MongoDB Articles

Page 87 of 111

Fetch records in MongoDB on querying its subset

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 181 Views

To fetch records in MongoDB that contain a subset of values within an array field, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of their order or position. Syntax db.collection.find({ "arrayField": { $all: ["value1", "value2", "value3"] } }); Sample Data db.subsetOfAnArrayDemo.insertOne({ "StudentProgrammingSkills": [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ...

Read More

Extract subarray value in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 466 Views

To extract a subarray value in MongoDB, you can use the $elemMatch projection operator to return only the first array element that matches the specified condition. Syntax db.collection.find( { query }, { arrayField: { $elemMatch: { field: "value" } } } ); Sample Data Let us first create a collection with documents ? db.extractSubArrayDemo.insertOne({ _id: 101, "clientName": "Larry", "ClientDetails": [ { ...

Read More

MongoDB query to get specific month|year (not date)?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 766 Views

To get specific month or year values from MongoDB date fields, use the aggregation framework with $month and $year operators. These operators extract the numeric month (1-12) and year values from date fields. Syntax db.collection.aggregate([ { $project: { field: 1, dateField: { $month: "$dateField" } } }, { $match: { dateField: monthNumber } } ]); Sample Data db.specificMonthDemo.insertMany([ { "StudentName": "Larry", "StudentDateOfBirth": new ISODate("1995-01-12") }, { "StudentName": "Chris", "StudentDateOfBirth": new ISODate("1999-12-31") }, { ...

Read More

How do I add a value to the top of an array in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 266 Views

To add a value to the top of an array in MongoDB, you can use the $push operator with $each and $position: 0 to insert elements at the beginning of an array field in a document. Syntax db.collection.updateOne( { query }, { $push: { arrayField: { $each: ["newValue"], ...

Read More

Finding matching records with LIKE in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 184 Views

To perform LIKE operations in MongoDB, use regular expressions (regex) with the find() method. MongoDB doesn't have a LIKE operator like SQL, but regex patterns provide equivalent functionality for pattern matching in text fields. Syntax db.collection.find({ "field": /pattern/flags }) Common patterns: /^text/ - Starts with "text" /text$/ - Ends with "text" /text/ - Contains "text" /text/i - Case-insensitive matching Sample Data db.likeDemo.insertMany([ {"Name": "John", "Age": 32}, {"Name": "Chris", "Age": 25}, {"Name": "Carol", "Age": 22}, ...

Read More

Converting isodate to numerical value in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 516 Views

In MongoDB, you can convert an ISODate to a numerical value (milliseconds since Unix epoch) using the getTime() method. This returns the timestamp as a number, which is useful for calculations and comparisons. Syntax yourDateVariable.getTime() Example Let's create an ISODate and convert it to a numerical value ? var arrivalDate = ISODate('2019-04-18 13:50:45'); Now convert the ISODate to numerical value ? arrivalDate.getTime(); 1555595445000 Verify Result To verify this is correct, convert the number back to ISODate ? new Date(1555595445000); ...

Read More

Creating alias in a MongoDB query?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 4K+ Views

To create an alias in a MongoDB query, use the aggregation framework with the $project stage. This allows you to rename fields in the output by mapping original field names to new alias names. Syntax db.collection.aggregate([ { $project: { _id: 1, "aliasName": "$originalFieldName" } } ]); Sample ...

Read More

Difference between find() and findOne() methods in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 5K+ Views

The findOne() method returns the first document if a query matches, otherwise returns null. The find() method never returns null — it always returns a cursor object, even when no documents match. Syntax // findOne() - returns single document or null db.collection.findOne(query, projection); // find() - returns cursor object db.collection.find(query, projection); Sample Data Let us create an empty collection to demonstrate the behavior ? db.createCollection('emptyCollection'); { "ok" : 1 } Check the document count in the collection ? db.emptyCollection.count(); 0 ...

Read More

How to remove a MySQL collection named 'group'?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 150 Views

The group is a reserved method name in MongoDB. While you can create a collection named "group", it requires special syntax to access. To remove such a collection, use getCollection('group').drop() method. Syntax db.getCollection('group').drop(); Create Sample Collection Let us first create a collection named "group" with some sample documents ? db.createCollection('group'); { "ok" : 1 } db.getCollection('group').insertMany([ {"StudentName": "Chris"}, {"StudentName": "Robert"} ]); { "acknowledged" : true, "insertedIds" : ...

Read More

How to apply a condition only if field exists in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 2K+ Views

To apply a condition only if a field exists in MongoDB, use the $or operator combined with $exists. This allows you to match documents where a field either doesn't exist or meets a specific condition. Syntax db.collection.find({ $or: [ { fieldName: { $exists: false } }, { fieldName: condition } ] }); Sample Data Let's create a collection with some documents where the StudentAge field is missing in one document ...

Read More
Showing 861–870 of 1,106 articles
« Prev 1 85 86 87 88 89 111 Next »
Advertisements