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 80 of 111
Find objects created in last week in MongoDB?
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 MoreFind the MongoDB document from sub array?
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 MoreIncrement a field in MongoDB document which is embedded?
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 MoreSelecting database inside the JS in MongoDB?
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 MoreCombine update and query parts to form the upserted document in MongoDB?
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 MoreMongoDB query to get record beginning with specific element from an array?
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 MoreAggregate a $slice to get an element in exact position from a nested array in MongoDB?
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 MoreWhat does createdCollectionAutomatically mean in MongoDB?
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 MoreMongoDB equivalent of `select distinct(name) from collectionName where age = "25"`?
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 MoreDelete data from a collection in MongoDB using multiple conditions?
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