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 68 of 111
Getting only the first item for an array property in MongoDB?
To get only the first item from an array property in MongoDB, use the $slice operator in the projection stage. This operator limits the number of array elements returned in the query result. Syntax db.collection.find( { query }, { arrayField: { $slice: 1 } } ); Sample Data db.gettingFirstItemInArrayDemo.insertOne({ "UserId": 101, "UserName": "Carol", "UserOtherDetails": [ {"UserFriendName": "Sam"}, ...
Read MoreHow to limit the amount of characters returned from a field in a MongoDB query?
To limit the amount of characters returned from a field in a MongoDB query, use the $substr operator within an aggregation pipeline. This operator extracts a substring from a string field based on starting position and character count. Syntax db.collection.aggregate([ { $project: { fieldName: { $substr: ["$originalField", startIndex, numberOfCharacters] } } } ]); Create Sample Data db.limitTheNumberOfCharactersDemo.insertMany([ ...
Read MoreHow to pop a single value in MongoDB?
To pop a single value in MongoDB, use the $pop operator to remove the first or last element from an array field. The $pop operator takes a value of 1 to remove the last element or -1 to remove the first element. Syntax db.collection.updateOne( { query }, { $pop: { "arrayField": 1 } } // Remove last element ); db.collection.updateOne( { query }, { $pop: { "arrayField": -1 } } // Remove first element ); ...
Read MoreHow to print results of script in MongoDB?
To print results of a script in MongoDB, use the printjson() method along with cursor iteration. This allows you to display query results in a formatted JSON structure, which is particularly useful when working with scripts or loops. Syntax var cursor = db.collection.find(); while (cursor.hasNext()) { printjson(cursor.next()); } Create Sample Data Let us first create a collection with documents ? db.printResultScriptDemo.insertMany([ {"StudentName": "John", "StudentAge": 21}, {"StudentName": "Carol", "StudentAge": 20}, {"StudentName": "David", "StudentAge": 19} ]); ...
Read MoreGroup all documents with common fields in MongoDB?
To group all documents with common fields in MongoDB, use the $group aggregation stage with $addToSet operator. This accumulates unique values from documents that share the same field value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldToGroupBy", "groupedField": { $addToSet: "$fieldToAccumulate" } } } ]); Sample Data ...
Read MoreCompare multiple properties in MongoDB?
To compare multiple properties in MongoDB, use the $where operator which allows JavaScript expressions to evaluate conditions between different fields or array elements within the same document. Syntax db.collection.find({ $where: "this.field1 > this.field2" }); Sample Data Let us create a collection with sample documents − db.comparingMultiplePropertiesDemo.insertOne({ "Values": [10, 70, 60] }); { "acknowledged": true, "insertedId": ObjectId("5cf228fcb64a577be5a2bc0a") } Display the document to verify insertion − db.comparingMultiplePropertiesDemo.find().pretty(); { ...
Read MoreDelete specific record from an array nested within another array in MongoDB?
To delete a specific record from an array nested within another array in MongoDB, use the $pull operator combined with the $ positional operator to target elements in nested arrays. Syntax db.collection.update( {"parentArray.field": "value"}, { $pull: { "parentArray.$.nestedArray": { "field": "matchValue" } } } ); Sample Data Let us first create a collection with nested array documents ? db.deletingSpecificRecordDemo.insertOne({ "StudentDetails": [ { ...
Read MoreSpecify return format in MongoDB to return the values as an array?
To specify return format in MongoDB and return values as an array, use the aggregation pipeline with the $group stage and $addToSet operator to collect distinct values into an array. Syntax db.collection.aggregate([ { "$group": { "_id": null, "fieldName": { "$addToSet": "$fieldPath" } } }, { ...
Read MoreUpdate key value where another key equals some value in MongoDB?
To update a key value where another key equals a specific value in MongoDB arrays, use $elemMatch to match the array element and $set with the positional operator $ to update the matched element. Syntax db.collection.update( { "arrayField": { "$elemMatch": { "matchKey": "matchValue" } } }, { "$set": { "arrayField.$.updateKey": "newValue" } } ); Sample Data Let us first create a collection with documents − db.keyValueDemo.insertOne({ "_id": new ObjectId(), "CustomerDetails": [ ...
Read MoreIs it possible to return a list of specific values from a MongoDB query?
Yes, you can return a list of specific values from a MongoDB query using the map() method or projection. The map() method transforms query results into an array of specific field values. Syntax db.collection.find().map(function(document){ return document.fieldName; }); Sample Data db.listOfSpecificValuesDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Chris"}, {"StudentName": "Robert"}, {"StudentName": "David"} ]); { "acknowledged": true, "insertedIds": { ...
Read More