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 31 of 111
How we can perform sort on ObjectId column in MongoDB?
To perform sort on ObjectId column in MongoDB, use the sort() method with the _id field. ObjectIds are sorted by their creation timestamp, making them useful for chronological ordering. Syntax db.collection.find().sort({_id: 1}); // Ascending (oldest first) db.collection.find().sort({_id: -1}); // Descending (newest first) Sample Data db.demo403.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "Bob"}, {"Name": "Adam"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreHow to get the intersection of two arrays in MongoDB?
To get intersection of two arrays in MongoDB, use $setIntersection along with aggregate(). This operator returns an array containing elements that appear in both input arrays. Syntax db.collection.aggregate([ { $project: { fieldName: { $setIntersection: ["$array1", "$array2"] } } } ]); Sample Data Let us create a collection with documents ? db.demo61.insertOne({ "Values1": ...
Read MoreMongoDB query to get date records in a range
To get date records in a range, use the $gt (greater than) and $lt (less than) operators together to define the date boundaries for your query. Syntax db.collection.find({ "dateField": { "$gt": ISODate("start-date"), "$lt": ISODate("end-date") } }); Sample Data db.demo60.insertMany([ {"ArrivalDate": new ISODate("2019-01-11 12:30:10")}, {"ArrivalDate": new ISODate("2019-10-12 03:10:00")}, {"ArrivalDate": new ISODate("2019-01-14 05:11:20")} ]); { ...
Read MoreSet multiple conditions in MongoDB and fetch value in a range
To set multiple conditions in MongoDB and fetch values in a range, use comparison operators like $gt (greater than) and $lt (less than) combined in a single query. This allows you to filter documents based on numeric ranges or multiple criteria. Syntax db.collection.find({ field: { $gt: minValue, $lt: maxValue } }); Sample Data db.demo59.insertMany([ { "Values": 50 }, { "Values": 10 }, { "Values": 58 }, { "Values": 78 } ]); ...
Read MoreHow to update a MongoDB document without overwriting the existing one?
To update only a field value in MongoDB, use update() along with $set. This won't overwrite the existing document but only modifies the specified fields. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Sample Data Let us first create a collection with documents ? db.demo401.insertOne({ "_id": 1001, "Name": "Chris", "SubjectName": "MongoDB", "Score": 45 }); { "acknowledged": true, "insertedId": 1001 ...
Read MoreMongoDB query to update only a single item from voting (up and down) records?
To update a single field in MongoDB voting records, use the $inc operator to increment or decrement vote counts. This allows you to modify only the TotalVote field without affecting other document properties. Syntax db.collection.update( { "field": "matchValue" }, { $inc: { "fieldToUpdate": incrementValue } } ); Create Sample Data Let us first create a collection with voting records − db.demo57.insertMany([ { "Votes": { "VoterName": "Chris", "TotalVote": 50 } }, { "Votes": { "VoterName": "David", "TotalVote": ...
Read MoreHow to copy attributes in MongoDB?
To copy the value of one attribute to another in MongoDB, use $set with forEach() to iterate through documents and update each one by copying the source field value to the target field. Syntax db.collection.find({}).forEach(function(doc) { db.collection.update( { _id: doc._id }, { $set: { targetField: doc.sourceField } } ); }); Sample Data db.demo55.insertMany([ { "ShippingDate": "", "date": new ISODate("2019-01-21") }, ...
Read MoreSet MongoDB $slice with a range?
To set slice with a range in MongoDB, use the $slice operator with two parameters: the starting position and the number of elements to return. This allows you to extract a specific range of elements from an array field. Syntax db.collection.find( {}, { "arrayField": { $slice: [startIndex, numberOfElements] } } ); Sample Data Let us create a collection with documents ? db.demo54.insertOne({ "ListOfValues": [100, 2030, 5353, 7364, 635, 535, 524, 423, 2434, 1323, 799874, 90] }); { ...
Read MoreMongoDB query to convert an array to a map of documents with n attributes?
To convert an array to a map of documents with n attributes in MongoDB, use the $arrayToObject operator combined with $map. This transforms array elements into key-value pairs where each document becomes a property of the resulting object. Syntax db.collection.aggregate([ { "$addFields": { "fieldName": { "$arrayToObject": { ...
Read MoreHow do I index "or" in MongoDB for indexing multiple fields?
To index multiple fields in MongoDB, use createIndex() (or the deprecated ensureIndex()) with a combination of fields. This creates compound indexes that can efficiently support queries on multiple field combinations. Syntax db.collection.createIndex({ "field1": 1, "field2": 1, "field3": -1 }); Where 1 indicates ascending order and -1 indicates descending order. Create Sample Data First, let's insert sample documents into the collection ? db.demo53.insertMany([ {"StudentFirstName": "Chris", "StudentAge": 21, "StudentCountryName": "US"}, {"StudentFirstName": "David", "StudentAge": ...
Read More