MongoDB Articles

Page 80 of 111

Find objects created in last week in MongoDB?

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

To find objects created in the last week in MongoDB, use the $gte operator with a date calculation that subtracts 7 days from the current date. This filters documents with dates greater than or equal to one week ago. Syntax db.collection.find({ "dateField": { $gte: new Date(new Date() - 7 * 60 * 60 * 24 * 1000) } }); Sample Data Let us first create a collection with documents to demonstrate the query ? db.findObjectInLastWeekDemo.insertMany([ ...

Read More

Find the MongoDB document from sub array?

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

To find documents from a sub array in MongoDB, use dot notation to access nested array elements. This allows you to query specific fields within arrays that are embedded in your documents. Syntax db.collection.find({ "parentField.arrayField.subField": "value" }); Sample Data Let us create sample documents with employee appraisal data ? db.findDocumentDemo.insertMany([ { "EmployeeDetails": { "EmployeeAppraisalTime": [ ...

Read More

Increment a field in MongoDB document which is embedded?

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

To increment a field in a MongoDB document that is embedded within nested objects, use the $inc operator with dot notation to specify the path to the embedded field. Syntax db.collection.update( { "query": "condition" }, { $inc: { "parent.child.field": incrementValue } } ); Sample Data Let's create a document with embedded StudentScores inside StudentDetails: db.embeddedValueIncrementDemo.insertOne({ "StudentDetails": { "StudentScores": { "StudentMathScore": ...

Read More

Selecting database inside the JS in MongoDB?

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

To select a database inside JavaScript in MongoDB, use the getSiblingDB() method with the var keyword to create a reference to another database from the current connection. Syntax var variableName = db.getSiblingDB('databaseName'); Example Let's select a database named 'sample' using getSiblingDB() ? selectedDatabase = db.getSiblingDB('sample'); sample Working with the Selected Database Now insert some documents into the selected database. Let's use a collection named 'selectDatabaseDemo' ? selectedDatabase.selectDatabaseDemo.insertMany([ {"ClientName": "John Smith", "ClientAge": 23}, {"ClientName": "Carol Taylor", "ClientAge": ...

Read More

Combine update and query parts to form the upserted document in MongoDB?

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

To combine update and query parts to form an upserted document in MongoDB, use the $set operator with upsert: true. This creates a new document if no match is found, or updates an existing one. Syntax db.collection.update( { queryCondition }, { $set: { field: "newValue" } }, { upsert: true } ); Sample Data db.updateWithUpsertDemo.insertMany([ { "StudentFirstName": "John", "StudentAge": 21 }, { "StudentFirstName": "Larry", "StudentAge": 23 }, { "StudentFirstName": ...

Read More

MongoDB query to get record beginning with specific element from an array?

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

To query records beginning with specific elements from an array in MongoDB, use dot notation with array index positions to match exact values at specific positions within the array. Syntax db.collection.find({ "arrayField.0": value1, "arrayField.1": value2, "arrayField.n": valueN }); Sample Data db.arrayStartsWithElementDemo.insertMany([ { "PlayerName": "Chris", "PlayerScore": [780, 9000, 456, 789, 987] }, { ...

Read More

Aggregate a $slice to get an element in exact position from a nested array in MongoDB?

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

To get an element from an exact position in a nested array using MongoDB's aggregation framework, use the $slice operator within a $project stage to extract specific elements by their index position. Syntax db.collection.aggregate([ { $project: { "arrayField": { $slice: ["$arrayField", startIndex, numberOfElements] } } } ]); Sample Data db.exactPositionDemo.insertOne({ "StudentName": "John", ...

Read More

What does createdCollectionAutomatically mean in MongoDB?

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

The createdCollectionAutomatically field in MongoDB indicates whether an operation automatically created a collection that didn't exist. When you perform operations like createIndex() or insert() on a non-existing collection, MongoDB creates the collection automatically and returns this boolean flag as true. Syntax db.nonExistingCollection.createIndex({fieldName: 1}); // Returns: { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Example 1: Creating Index on Non-Existing Collection Let us create an index on a collection that doesn't exist ? db.createCollectionDemo.createIndex({"ClientCountryName": ...

Read More

MongoDB equivalent of `select distinct(name) from collectionName where age = "25"`?

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

To get the MongoDB equivalent of select distinct(name) from collectionName where age = "25", use the distinct() method with a field name and query condition. This returns unique values from the specified field that match the given criteria. Syntax db.collection.distinct("fieldName", { "conditionField": value }) Sample Data db.distinctNameAndAgeDemo.insertMany([ { "ClientFirstName": "John", "Age": 23 }, { "ClientFirstName": "Larry", "Age": 25 }, { "ClientFirstName": "David", "Age": 25 }, { "ClientFirstName": "Carol", "Age": 26 }, { "ClientFirstName": ...

Read More

Delete data from a collection in MongoDB using multiple conditions?

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

To delete data from a MongoDB collection using multiple conditions, use deleteOne() or deleteMany() with a query document containing multiple field-value pairs. All conditions must match for the document to be deleted. Syntax db.collection.deleteOne({ "field1": "value1", "field2": "value2" }); Sample Data db.deleteDataDemo.insertMany([ {_id: 1, "Name": "Larry"}, {_id: 2, "Name": "Chris"}, {_id: 3, "Name": "Robert"}, {_id: 4, "Name": "David"}, {_id: 5, "Name": "Carol"}, ...

Read More
Showing 791–800 of 1,106 articles
« Prev 1 78 79 80 81 82 111 Next »
Advertisements