Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
MongoDB Articles
Page 86 of 111
Create array with MongoDB query?
You can use the toArray() method with MongoDB queries to convert query results into an array format. This method transforms the cursor returned by find() into a JavaScript array containing all matching documents. Syntax db.collectionName.find({}, {fieldName: 1}).toArray(); Sample Data Let us create a collection with sample documents ? db.createArrayDemo.insertMany([ {"UserName": "Chris"}, {"UserName": "David"}, {"UserName": "Robert"}, {"UserName": "Sam"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreCount distinct value in MongoDB?
To count distinct values in MongoDB, use the distinct() method combined with the .length property. This returns the number of unique values for a specified field across all documents in a collection. Syntax db.collectionName.distinct("fieldName").length; Create Sample Data Let us create a collection with documents that contain duplicate values ? db.countDistinctDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Chris"}, {"StudentName": "Chris"}, {"StudentName": "Carol"}, {"StudentName": "David"}, {"StudentName": "Carol"} ]); { ...
Read MoreGet index of given element in array field in MongoDB?
To get the index of a given element in an array field in MongoDB, use the $indexOfArray operator within an aggregation pipeline. This operator returns the zero-based index of the first occurrence of the specified element in the array. Syntax db.collection.aggregate([ { $project: { "fieldName": { $indexOfArray: [ "$arrayField", "searchElement" ] ...
Read MoreHow to query MongoDB using the $ne operator?
To query MongoDB using the $ne operator, use it to find documents where a field is not equal to a specified value. The $ne operator excludes documents that match the given value. Syntax db.collection.find({fieldName: {$ne: value}}); Create Sample Data Let us create a collection with student documents ? db.notEqualToDemo.insertMany([ {"StudentName": "Larry", "StudentMathMarks": 68}, {"StudentName": "Chris", "StudentMathMarks": 88}, {"StudentName": "David", "StudentMathMarks": 45}, {"StudentName": "Carol", "StudentMathMarks": 69} ]); { "acknowledged": true, ...
Read MoreUnset an attribute from a single array element in MongoDB?
To unset an attribute from a single array element in MongoDB, use the $unset operator combined with the $ positional operator to target the specific array element that matches your query condition. Syntax db.collection.update( {"arrayField.attribute": "matchValue"}, {$unset: {"arrayField.$.attribute": 1}} ) Create Sample Data db.unsetAnAttributeDemo.insertOne({ _id: 1, "StudentDetails": [ { "StudentFirstName": "Ramit", ...
Read MoreSearch by property name for any document with that property in MongoDB?
To search for documents that contain a specific property in MongoDB, use the $ne operator with a null value. This will return all documents where the property exists and is not null. Syntax db.collection.find({ propertyName: { $ne: null } }) Sample Data db.searchByPropertyName.insertMany([ { "FirstName": "Larry", "Age": 23 }, { "FirstName": null, "Age": 21 }, { "FirstName": "John", "Age": 22 }, { "FirstName": null, "Age": 25 }, { "FirstName": "David", "Age": 20 } ...
Read MoreMongoDB query to find the highest numeric value of a column?
To find the highest numeric value of a column in MongoDB, use $type operator with $not to filter only numeric values, then apply sort() and limit() to get the maximum value. Syntax db.collection.find({ "field": { $not: { $type: "string" } } }).sort({ "field": -1 }).limit(1); Sample Data db.highestNumericValueOfAColumnDemo.insertMany([ { "StudentName": "John", "StudentMathMarks": 69 }, { ...
Read MoreDelete a field and value in MongoDB?
To delete a field and its value from MongoDB documents, use the $unset operator. This operator removes the specified field from all matched documents in the collection. Syntax db.collection.update( { field: { $exists: true } }, { $unset: { fieldName: 1 } }, { multi: true } ) Sample Data Let us first create a collection with sample documents − db.deleteFieldDemo.insertMany([ { "FirstName": "John", "LastName": "Smith" }, { "FirstName": "David", "LastName": "Miller" ...
Read MoreHow to read a specific key-value pair from a MongoDB collection?
To read a specific key-value pair from a MongoDB collection, use dot notation in the projection parameter of the find() method. This allows you to retrieve only the nested fields you need. Syntax db.collection.find( {}, { "parentField.childField": 1 } ); Sample Data db.readSpecificKeyValueDemo.insertOne({ "_id": 100, "StudentDetails": { "StudentFirstName": "David", "StudentLastName": "Miller", "StudentAge": 23, ...
Read MoreHow to remove duplicate values inside a list in MongoDB?
To remove duplicate values from an array field in MongoDB, use the aggregation framework with the $setUnion operator. This operator creates a set union between the array and an empty array, automatically eliminating duplicates. Syntax db.collection.aggregate([ { $project: { "fieldName": { $setUnion: ["$arrayField", []] } } } ]); Sample Data db.removeDuplicatesDemo.insertOne({ "InstructorName": "Chris", ...
Read More