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
Articles by Nishtha Thakur
Page 11 of 40
Find 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 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 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 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 MoreMongoDB query to pull array element from a collection?
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 MoreWhat is return type of db.collection.find() in MongoDB?
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 MoreQuery MongoDB collection starting with _?
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 MoreEscaping quotes while inserting records in MongoDB?
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 MoreHow to prepend string to entire column in MongoDB?
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 MoreHow to find datatype of all the fields in MongoDB?
To find the datatype of all fields in MongoDB, use the typeof operator with findOne() to check individual field types, or use aggregation pipeline to analyze multiple fields systematically. Syntax typeof db.collectionName.findOne().fieldName; Sample Data Let us first create a collection with documents ? db.findDataTypeDemo.insertOne({ "ClientName": "Chris", "isMarried": false, "age": 25, "salary": 50000.50 }); { "acknowledged": true, "insertedId": ObjectId("5ccf2064dceb9a92e6aa1952") } Display the document to ...
Read More