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
Database Articles
Page 410 of 546
How to get all collections where collection name like '%2015%'?
Let us first create some collections that starts from year number i.e. 2015, 2019, etc −> use web; switched to db web > db.createCollection("2015-myCollection"); { "ok" : 1 } > db.createCollection("2019-employeeCollection"); { "ok" : 1 } > db.createCollection("2015-yourCollection"); { "ok" : 1 }Now you can display all the collections with the help of SHOW −> show collections;This will produce the following output −2015-myCollection 2015-yourCollection 2019-employeeCollection applyConditionDemo check creatingAliasDemo emp_info emptyCollection removeNullDemoFollowing is the query to get all collections where collection name like ‘%2015%’ −> db.getCollectionNames().filter(function (v) { return /^2015\-/.test(v); })This will produce the following output −[ "2015-myCollection", "2015-yourCollection" ]If you ...
Read MoreHow to check if a field in MongoDB is [] or {}?
To check if a field in MongoDB is [] or {}, you can use the following syntax −db.yourCollectionName.find({ "yourOuterFieldName": { "$gt": {} }, "yourOuterFieldName.0": { "$exists": false } });Let us first create a collection with documents -> db.checkFieldDemo.insert([ ... { _id: 1010, StudentDetails: {} }, ... { _id: 1011, StudentDetails: [ { StudentId: 1 } ] }, ... { _id: 1012, StudentDetails: [ {} ] }, ... { _id: 1013 }, ... { _id: 1014, StudentDetails: null}, ... { _id: 1015, StudentDetails: { StudentId: 1 } } ... ]); BulkWriteResult({ "writeErrors" ...
Read MoreHow to define aliases in the MongoDB Shell?
To define aliases in MongoDB shell, you can use below syntax −Object.defineProperty(this, 'yourFunctionName', { get: function() { yourStatement1, . . return N }, enumerable: true, configurable: true });Following is the syntax to assign with var −var anyAliasName=yourFunctionName;Let us implement the above syntax in order to define an aliases in the MongoDB shell. Here, 'displayMessageDemo' is our function −> Object.defineProperty(this, 'displayMessageDemo', { ... get: function() { ... return "Hello MongoDB" ... }, ... enumerable: true, ... configurable: true ... });Query to assign function to var in MongoDB shell −> var myMessage = displayMessageDemo;Let us display the value of above aliases −> myMessage;This will produce the following output −Hello MongoDB
Read MoreSearch a sub-field on MongoDB?
To search a sub-filed in MongoDB, you can use double quotes along with dot notation. Let us first create a collection with documents −> db.searchSubFieldDemo.insertOne( ... { ... "UserDetails": ... {"UserEmailId":"John123@gmail.com", "UserAge":21} ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd3d909edc6604c74817ce2") } > db.searchSubFieldDemo.insertOne( { "UserDetails": {"UserEmailId":"Carol@yahoo.com", "UserAge":26} } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd3d9a4edc6604c74817ce3") }Following is the query to display all documents from a collection with the help of find() method −> db.searchSubFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd3d909edc6604c74817ce2"), "UserDetails" ...
Read MoreMongoDB pull with positional operator?
Use $pull operator along with positional operator($) in MongoDB. Let us first create a collection with documents −> db.pullWithPositionalOperatorDemo.insertOne( ... { ... _id: 100, ... "StudentDetails": [ ... { ... "StudentId": "STU-1", ... "StudentFavouriteSubject": ["MongoDB", "Java"] ... }, ... { ... "StudentId": "STU-2", ... "StudentFavouriteSubject": ["PHP", "MySQL"] ... } ... ] ... } ... ); { ...
Read MoreFind all documents that have two specific id's in an array of objects in MongoDB?
You can use $and operator for this. Let us first create a collection with documents −> db.twoSpecificIdsDemo.insertOne( ... { ... PlayerId:1, ... "PlayerDetails": [{ ... id: 100, ... "PlayerName":"Chris" ... }, { ... id: 101, ... "PlayerName":"Sam" ... }, { ... id: 102, ... "PlayerName":"Robert" ... }, { ... id: 103, ... "PlayerName":"Carol" ... }] ...
Read MoreInsert MongoDB document field only when it's missing?
Let us first create a collection with documents −>db.missingDocumentDemo.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fb1eedc6604c74817ce6") } >db.missingDocumentDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fb29edc6604c74817ce7") } >db.missingDocumentDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fb40edc6604c74817ce8") }Following is the query to display all documents from a collection with the help of find() method −> db.missingDocumentDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd3fb1eedc6604c74817ce6"), "StudentFirstName" : "Adam", "StudentLastName" : "Smith" } { "_id" : ObjectId("5cd3fb29edc6604c74817ce7"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" } { "_id" : ...
Read MoreHow to search for documents based on the value of adding two properties in MongoDB?
You can use aggregate framework for this. Here, we will get the sum and then match it to search for documents less than a particular number. Let us first create a collection with documents −> db.searchDocumentsDemo.insertOne({"Value1":100, "Value2":560}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe1eedc6604c74817ce9") } > db.searchDocumentsDemo.insertOne({"Value1":300, "Value2":150}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe29edc6604c74817cea") } > db.searchDocumentsDemo.insertOne({"Value1":400, "Value2":200}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe30edc6604c74817ceb") } > db.searchDocumentsDemo.insertOne({"Value1":190, "Value2":210}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3fe45edc6604c74817cec") }Following is the query to display all documents from a collection with the help of ...
Read MoreQuery to retrieve multiple items in an array in MongoDB?
To retrieve multiple items in an array, use aggregate framework. Let us first create a collection with documents −> db.retrieveMultipleDemo.insertOne( ... { ... "UserDetails": ... [ ... { "_id": "101", "UserName":"John", "UserAge": 23 }, ... { "_id": "102", "UserName":"Carol", "UserAge": 21 }, ... { "_id": "103", "UserName":"David", "UserAge": 23}, ... { "_id": "104", "UserName":"Sam", "UserAge": 25} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd40c85edc6604c74817cf0") }Following is the query to ...
Read MoreCalculate average of ratings in array and then include the field to original document in MongoDB?
You can use $avg operator along with aggregate framework. Let us first create a collection with documents −> db.averageOfRatingsInArrayDemo.insertOne( ... { ... "StudentDetails":[ ... { ... "StudentId":1, ... "StudentScore":45 ... }, ... { ... "StudentId":2, ... "StudentScore":58 ... }, ... { ... "StudentId":3, ... ...
Read More