Found 6702 Articles for Database

MongoDB query to test if a value is in array?

Naveen Singh
Updated on 27-Mar-2020 12:02:38

360 Views

To check for a specific value, use $in. Let us first create a collection with documents −> db.testInArray.insertOne({"ListOfNumbers":[10, 56, 78, 90, 32]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d42df5e889d7a519950d") } > db.testInArray.insertOne({"ListOfNumbers":[56, 78, 91, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d588f5e889d7a519950e") }Following is the query to display all documents from a collection with the help of find() method −> db.testInArray.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e04d42df5e889d7a519950d"),    "ListOfNumbers" : [       10,       56,       78,       90,       32   ... Read More

How to get specific data in different formats with MongoDB?

Naveen Singh
Updated on 27-Mar-2020 11:59:38

65 Views

For this, simply use find(). For a different format, use pretty(). Let us first create a collection with documents −> db.getSpecificData.insertOne( ... { ...    "StudentName": "John", ...    "Information": { ...       "FatherName": "Chris", ...       "Place": { ...          "CountryName": "US", ...          "ZipCode":"111344" ...       }, ...       "id": "1" ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e039abdf5e889d7a5199509") } > db.getSpecificData.insertOne( ...    { ...       "StudentName": "Carol", ...       "Information": ... Read More

MongoDB - How to copy rows into a newly created collection?

Naveen Singh
Updated on 27-Mar-2020 11:54:39

268 Views

To copy rows into another collection, use MongoDB. The syntax is as follows wherein “yourOldCollectionName” is the old collection, whereas where this collection will get copied is our new collection i.e. “yourNewCollectionName” −db.yourOldCollectionName.aggregate([{ $sample: { size: 333333 }}, {$out: "yourNewCollectionName"} ], {allowDiskUse: true});Let us first create a collection with documents −> db.sourceCollection.insertOne({"EmployeeName":"Robert", "EmployeeSalary":15000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0397c1f5e889d7a5199506") } > db.sourceCollection.insertOne({"EmployeeName":"David", "EmployeeSalary":25000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0397c3f5e889d7a5199507") } > db.sourceCollection.insertOne({"EmployeeName":"Mike", "EmployeeSalary":29000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0397c4f5e889d7a5199508") }Following is the query to display all documents from a collection with ... Read More

Why is MongoDB taking too much time to find the record?

Naveen Singh
Updated on 27-Mar-2020 11:52:36

182 Views

In this case, use the concept of index on a particular field. Let us first create a collection with documents. Here, we have created index as well using createIndex() −> db.decreasetimeusingindex.createIndex({"StudentName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.decreasetimeusingindex.insertOne({"StudentName":"John Smith", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e03960af5e889d7a51994ff") } > db.decreasetimeusingindex.insertOne({"StudentName":"John Doe", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e039611f5e889d7a5199500") } > db.decreasetimeusingindex.insertOne({"StudentName":"Adam Smith", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e03961df5e889d7a5199501") }Following is the query to display all documents from a ... Read More

Extract a particular element from a nested array in MongoDB

Naveen Singh
Updated on 27-Mar-2020 11:49:28

145 Views

Extract a particular element from a nested array with the help of dot(.) notation. Let us first create a collection with documents −> db.extractParticularElementDemo.insertOne( ...    { ...       "_id" : 101, ...       "StudentName" : "John", ...       "StudentInformation" : [ ...          { ...             "Age" : 21, ...             "StudentPersonalInformation" : [ ...                { ...                   "StudentNickName" : "Mike", ...       ... Read More

Replace value with a string literal during MongoDB aggregation operation

Naveen Singh
Updated on 27-Mar-2020 11:40:04

282 Views

Use MongoDB $literal to set a string literal. Let us first create a collection with documents −>db.replacevaluedemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":{"TeacherName":"Bob", "SubjectCode":"MySQL111"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0390a3f5e889d7a51994fd") } >db.replacevaluedemo.insertOne({"StudentName":"Mike", "StudentFavouriteSubject":{"TeacherName":"David", "SubjectCode":"3221Java"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0390b8f5e889d7a51994fe") }Following is the query to display all documents from a collection with the help of find() method −> db.replacevaluedemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e0390a3f5e889d7a51994fd"),    "StudentName" : "Chris",    "StudentFavouriteSubject" : {       "TeacherName" : "Bob",       "SubjectCode" : "MySQL111"    } } {    "_id" : ObjectId("5e0390b8f5e889d7a51994fe"),    "StudentName" ... Read More

How to count and sum a field between 2 dates in MongoDB?

Naveen Singh
Updated on 27-Mar-2020 11:37:24

1K+ Views

Use aggregation $gte and $lte along with $sum to count and sum a field between 2 dates. Let us first create a collection with documents −> db.countandsumdemo.insertOne({"Value":10, "created_at":ISODate('2019-10-11')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e6df5e889d7a51994fa") } > db.countandsumdemo.insertOne({"Value":50, "created_at":ISODate('2019-01-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e77f5e889d7a51994fb") } > db.countandsumdemo.insertOne({"Value":100, "created_at":ISODate('2019-06-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e8af5e889d7a51994fc") }Following is the query to display all documents from a collection with the help of find() method −> db.countandsumdemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e038e6df5e889d7a51994fa"),    "Value" : 10,    "created_at" : ISODate("2019-10-11T00:00:00Z") ... Read More

Build index in the background with MongoDB

Naveen Singh
Updated on 27-Mar-2020 11:32:25

2K+ Views

To create index in the background, use createIndex() method and set “background: true” as in the below syntax −db.yourCollectionName.createIndex({"yourFieldName1":1,"yourFieldName2":1},{background: true} );Let us implement the above syntax in order to create index and set background −> db.indexCreationDemo.createIndex({"StudentName":1,"StudentAge":1},{background: true} ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us display the indexes −> db.indexCreationDemo.getIndexes();This will produce the following output −[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_",       "ns" : "web.indexCreationDemo"    },    {       "v" : 2,       "key" : {          "StudentName" : 1,          "StudentAge" : 1       },       "name" : "StudentName_1_StudentAge_1",       "ns" : "web.indexCreationDemo",       "background" : true    } ]

Fix: MongoDB Robomongo: db.data.find(…).collation is not a function?

Naveen Singh
Updated on 27-Mar-2020 11:29:35

69 Views

The collation introduced in version MongoDB 3.4. Maybe, you implemented collation in a previous version.For our example, we are using MongoDB version 4.0.5. Following is the query to check the current version on system −> db.version()This will produce the following output −4.0.5Let us first create a collection with documents −> db.collationExample.createIndex({Value: 1}, {collation: {locale: "en", strength: 1}}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.collationExample.insertOne({'Value':'x'}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038a3cf5e889d7a51994f5") } > db.collationExample.insertOne({'Value':'X'}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038a48f5e889d7a51994f6") } > ... Read More

Calculating average value per document with sort in MongoDB?

Naveen Singh
Updated on 27-Mar-2020 11:26:56

148 Views

To calculate average, use aggregate along with $avg. Let us first create a collection with documents −> db.calculateAverage.insertOne({'Value':[10, 20, 80]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0383e3f5e889d7a51994dc") } > db.calculateAverage.insertOne({'Value':[12, 15, 16]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0383edf5e889d7a51994dd") } > db.calculateAverage.insertOne({'Value':[30, 35, 40]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0383f5f5e889d7a51994de") }Following is the query to display all documents from a collection with the help of find() method −> db.calculateAverage.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e0383e3f5e889d7a51994dc"),    "Value" : [       10,       20,     ... Read More

Advertisements