Database Articles

Page 84 of 547

Display distinctly ordered MongoDB record with skip and limit?

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

To display distinctly ordered MongoDB records with skip and limit, use the aggregation framework combining $group, $sort, $skip, and $limit operations. This approach ensures unique values while maintaining order and pagination control. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName" }}, { $sort: { _id: 1 }}, { $skip: numberOfRecordsToSkip }, { $limit: maxRecordsToReturn } ]); Sample Data db.orderedDistinctDemo.insertMany([ {"Name": "John"}, {"Name": "Larry"}, {"Name": "Larry"}, ...

Read More

How to delete a table from MongoDB database?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 594 Views

In MongoDB, collections are equivalent to tables in relational databases. To delete a collection (table) from a MongoDB database, use the drop() method, which removes the entire collection and all its documents permanently. Syntax db.collectionName.drop() Create Sample Data Let us first create a collection with some sample documents ? db.deleteTableDemo.insertMany([ {"Name": "Chris", "Age": 23}, {"Name": "Carol", "Age": 21}, {"Name": "David", "Age": 24} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Query MongoDB collection starting with _?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 304 Views

To work with MongoDB collections that have names starting with underscore (_), you need to use the getCollection() method instead of the standard dot notation, as collection names beginning with underscore require special handling. Syntax Create collection starting with underscore: db.createCollection('_yourCollectionName'); Insert documents into underscore collection: db.getCollection('_yourCollectionName').insertOne({ "field1": "value1", "field2": "value2" }); Query documents from underscore collection: db.getCollection('_yourCollectionName').find(); Example Let us create a collection starting with underscore and insert sample documents: db.createCollection('_testUnderscoreCollectionDemo'); ...

Read More

How to retrieve a nested object in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 1K+ Views

To retrieve a nested object in MongoDB, use the $ positional operator with dot notation to match specific elements within arrays or embedded documents. Syntax db.collection.find( {"arrayField.nestedField": value}, {"arrayField.$": 1} ) Sample Data Let us first create a collection with documents ? db.queryNestedObject.insertOne({ "StudentName": "James", "StudentSubjectScore": [ {"StudentMongoDBScore": 98}, {"StudentCScore": 92}, {"StudentJavaScore": ...

Read More

Update Array element in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 287 Views

To update an array element in MongoDB, you can use various operators like $set, $addToSet, or $push combined with the positional operator $ to target specific array elements. Syntax db.collection.update( {"arrayField.elementField": "matchValue"}, { $set: {"arrayField.$.newField": "newValue"} } ); Sample Data db.updateArrayDemo.insertOne({ "ClientDetails": [ { "ClientName": "John", "DeveloperDetails": [] ...

Read More

Escaping quotes while inserting records in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To escape quotes while inserting records in MongoDB, use the Unicode escape sequence \u0022 to represent double quotes within string values. This prevents syntax errors when quotes are part of the data content. Syntax db.collection.insertOne({ "fieldName": "text with \u0022 escaped quotes \u0022" }); Sample Data Let us create a collection with documents containing escaped quotes in student names − db.escapingQuotesDemo.insertMany([ { "StudentFullName": "John \u0022 Smith" }, { "StudentFullName": "David \u0022 Miller" }, { "StudentFullName": "John \u0022 ...

Read More

Implement multiple conditions in MongoDB?

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

To implement multiple conditions in MongoDB queries, use logical operators like $and, $or, and $nor to combine different criteria. These operators allow you to create complex queries that match documents based on multiple field conditions. Syntax // AND condition (all conditions must be true) db.collection.find({ $and: [{ condition1 }, { condition2 }] }); // OR condition (any condition can be true) db.collection.find({ $or: [{ condition1 }, { condition2 }] }); // NOR condition (none of the conditions should be true) db.collection.find({ $nor: [{ condition1 }, { condition2 }] }); Sample Data ...

Read More

Is it possible to achieve a slice chain in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 176 Views

Yes, you can achieve slice chaining in MongoDB using the aggregation framework. The $unwind operator can be applied multiple times to flatten nested arrays, allowing you to access and manipulate deeply nested elements. Syntax db.collection.aggregate([ { $match: { "field": "value" } }, { $unwind: "$arrayField" }, { $unwind: "$arrayField" }, { $group: { "_id": "$field", "result": { $last: "$arrayField" } } } ]); Sample Data db.sliceOfSliceDemo.insertOne({ "Name": "John", "Details": ...

Read More

How to prepend string to entire column in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 248 Views

To prepend a string to an entire column in MongoDB, use the $concat operator within the aggregation framework. The $concat operator combines the prepend string with the existing field value. Syntax db.collection.aggregate([ { $project: { "fieldName": { $concat: ["prependString", "$fieldName"] } ...

Read More

How to perform intersection of sets between the documents in a single collection in MongoDB?

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

To find the intersection of sets between documents in a single MongoDB collection, use the $setIntersection operator within an aggregation pipeline. This operator returns an array containing elements that appear in all specified arrays. Syntax { $project: { "result": { $setIntersection: ["$array1", "$array2", "$array3", ...] } } } Sample Data db.setInterSectionDemo.insertMany([ {"_id": 101, ...

Read More
Showing 831–840 of 5,468 articles
« Prev 1 82 83 84 85 86 547 Next »
Advertisements