MongoDB Articles

Page 81 of 111

Query for multiple parameters in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To query for multiple parameters in MongoDB, use multiple field-value pairs in the query document separated by commas. For nested fields, use dot notation to access specific properties within embedded documents or arrays. Syntax db.collection.find({ "field1": "value1", "field2": "value2", "nestedField.subField": "value3" }); Sample Data db.multipleParametersDemo.insertMany([ { "CustomerName": "Larry", "CustomerDetails": [ ...

Read More

MongoDB query to pull array element from a collection?

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

Use the $pull operator to remove specific values from an array field in MongoDB. The $pull operator removes all instances of a value from an existing array that match a specified condition. Syntax db.collection.update( { query }, { $pull: { arrayField: value } } ); Sample Data Let us first create a collection with a document containing an array ? db.pullElementFromAnArrayDemo.insertOne({ "StudentScores": [89, 56, 78, 90] }); { "acknowledged": true, ...

Read More

How do I add a field to existing record in MongoDB?

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

You can use the update command with the $set operator to add a field to existing records in MongoDB. This operation can add fields to specific documents or to all documents in a collection. Syntax db.collection.update( { query }, { $set: { "newFieldName": value } }, { multi: true } ); Sample Data db.addAFieldToEveryRecordDemo.insertMany([ { "ClientName": "Chris", "ClientAge": 34 }, { "ClientName": "Robert", "ClientAge": 36 } ]); { ...

Read More

What is return type of db.collection.find() in MongoDB?

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

The db.collection.find() method in MongoDB returns a cursor object, not the actual documents. A cursor is an iterable object that points to the result set of a query and allows you to traverse through the documents. Syntax db.collection.find(query, projection) The cursor returned can be used to iterate over results or apply additional methods like limit(), sort(), etc. Sample Data Let us first create a collection with documents ? db.findCursorDemo.insertMany([ {"ClientFirstName": "John", "ClientLastName": "Smith"}, {"ClientFirstName": "Carol", "ClientLastName": "Taylor"}, {"ClientFirstName": "David", ...

Read More

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 595 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 288 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
Showing 801–810 of 1,106 articles
« Prev 1 79 80 81 82 83 111 Next »
Advertisements