Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB query to return only embedded document?

George John
George John
Updated on 15-Mar-2026 461 Views

While MongoDB doesn't return only embedded documents directly, you can use projection to return specific fields and the $elemMatch operator to filter array elements that match certain conditions. Syntax db.collection.find( { "arrayField.nestedField": { $gte: value } }, { "arrayField": { $elemMatch: { "nestedField": { $gte: value } } } } ); Sample Data db.queryToEmbeddedDocument.insertOne({ "UserName": "Larry", "PostDetails": [ { "UserMessage": "Hello", "UserLikes": 8 }, ...

Read More

Search a string with special characters in a MongoDB document?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 2K+ Views

To search a string with special characters in MongoDB document, you can use backslash (\) to escape the special character in a regular expression. Here, we have special character $ in our string. Syntax db.collection.find({ "fieldName": /.*\specialCharacter.*/i }); Sample Data Let us first create a collection with documents ? db.searchDocumentWithSpecialCharactersDemo.insertMany([ { "UserId": "Smith$John123", "UserFirstName": "John", "UserLastName": "Smith" ...

Read More

Can we use NOT and AND together in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 291 Views

Yes, we can use NOT and AND together in MongoDB. MongoDB doesn't have direct NOT and AND operators, but we achieve this using $or and $ne operators following De Morgan's law: NOT (X AND Y) = NOT X OR NOT Y. Syntax db.collection.find({ "$or": [ {"field1": {"$ne": "value1"}}, {"field2": {"$ne": "value2"}} ] }); Sample Data db.NotAndDemo.insertMany([ {"StudentName": "John", "StudentCountryName": "US"}, {"StudentName": ...

Read More

How to remove document on the basis of _id in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 302 Views

To remove a document based on its _id in MongoDB, use the remove() method with the ObjectId as the matching criteria. Each document has a unique _id field that serves as the primary key. Syntax db.collectionName.remove({"_id": ObjectId("yourObjectId")}); Sample Data Let us first create a collection with sample documents ? db.removeDocumentOnBasisOfId.insertMany([ {"UserName": "Larry", "UserAge": 23, "UserCountryName": "US"}, {"UserName": "Sam", "UserAge": 21, "UserCountryName": "UK"}, {"UserName": "Chris", "UserAge": 24, "UserCountryName": "US"}, {"UserName": "Robert", "UserAge": 26, "UserCountryName": "UK"}, ...

Read More

Increment a value in a MongoDB nested object?

George John
George John
Updated on 15-Mar-2026 546 Views

To increment a value in a MongoDB nested object, use the $inc operator combined with the $ positional operator to target the specific nested element and increment its numeric value. Syntax db.collection.update( {"nestedArray.field": "matchValue"}, { $inc: { "nestedArray.$.fieldToIncrement": incrementValue } } ); Create Sample Data Let us create a collection with a document containing nested array objects ? db.incrementValueDemo.insertOne({ "StudentName": "Larry", "StudentCountryName": "US", "StudentDetails": [ ...

Read More

Inserting the current datetime in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 3K+ Views

To insert current datetime in MongoDB, use new Date() during insertion or $setOnInsert with upsert operations. MongoDB stores dates in ISODate format with UTC timezone. Syntax // Direct insertion db.collection.insertOne({ fieldName: new Date() }); // With upsert operation db.collection.update( { _id: value }, { $set: { field1: "value" }, $setOnInsert: { dateField: new Date() } }, { upsert: true } ); Sample Data db.addCurrentDateTimeDemo.insertMany([ { "StudentName": "John", "StudentAdmissionDate": new Date("2012-01-21") }, { ...

Read More

How to search document in MongoDB by _id

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 487 Views

To search a document in MongoDB by _id, you need to use the ObjectId() function to wrap the _id value, since MongoDB stores document IDs as ObjectId objects. Syntax db.collectionName.find({"_id": ObjectId("your_object_id_here")}); Create Sample Data Let us create a collection with sample documents ? db.searchDocumentDemo.insertMany([ {"UserId": 1, "UserName": "Larry"}, {"UserId": 2, "UserName": "Mike"}, {"UserId": 3, "UserName": "David"}, {"UserId": 4, "UserName": "Chris"}, {"UserId": 5, "UserName": "Robert"}, {"UserId": 6, "UserName": "Sam"} ...

Read More

How to create double nested array in MongoDB?

George John
George John
Updated on 15-Mar-2026 391 Views

To create a double nested array in MongoDB, use insertOne() or insertMany() with documents containing arrays of objects that themselves contain arrays. A double nested array means an array within an array structure. Syntax db.collection.insertOne({ "field1": "value1", "outerArray": [ { "innerField": "value", "innerArray": [ ...

Read More

Get the array of _id in MongoDB?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 839 Views

In MongoDB, the _id field is a mandatory field that acts as the primary key for each document. To retrieve documents by matching their _id values against an array of specific values, use the $in operator. Syntax db.collectionName.find({ _id: { $in: [value1, value2, value3, ...] } }); Create Sample Data db.selectInWhereIdDemo.insertMany([ {"_id": 23}, {"_id": 28}, {"_id": 45}, {"_id": 75}, {"_id": 85}, {"_id": 145} ]); { ...

Read More

Get all the column names in a table in MongoDB

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

In MongoDB, there is no concept of columns since MongoDB is schema-less and does not contain tables. It contains the concept of collections and a collection has different types of documents with varying field structures. Syntax // Insert documents with different field structures db.collectionName.insertOne({"fieldName1": "value1", "fieldName2": "value2"}); // Get all field names from a single document db.collectionName.findOne(); // Get all documents to see different field structures db.collectionName.find(); Sample Data Let us create a collection with documents having different field structures ? db.collectionOnDifferentDocumentDemo.insertMany([ {"UserName": "Larry"}, ...

Read More
Showing 23731–23740 of 61,297 articles
Advertisements