Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Database Articles - Page 123 of 671
800 Views
To count array elements from a specific field, use $size in MongoDB. Let us create a collection with documents −> db.demo723.insertOne({"Subject":["MySQL", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eab094d43417811278f588a") } > db.demo723.insertOne({"Subject":["C"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eab095243417811278f588b") } > db.demo723.insertOne({"Subject":["C++", "Java", "Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5eab095f43417811278f588c") }Display all documents from a collection with the help of find() method −> db.demo723.find();This will produce the following output −{ "_id" : ObjectId("5eab094d43417811278f588a"), "Subject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5eab095243417811278f588b"), "Subject" : [ "C" ] } { "_id" : ... Read More
3K+ Views
To ignore null values in MongoDB, use "$ne" : null in aggregate(). Let us create a collection with documents −> db.demo722.insertOne( ... { ... id:101, ... details: [ ... { Name:""}, ... { Name: "David"}, ... {Name:null}, ... {Name:"Carol"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eab07d543417811278f5889") }Display all documents from a collection with the help of find() method −> db.demo722.find();This will produce the following output ... Read More
844 Views
To count by multiple fields, use $facet in MongoDB. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Let us create a collection with documents −> db.demo721.insertOne( ... { ... ... "details1": { ... "id":101 ... ... }, ... "details2": { ... "id":101 ... }, ... "details3": { ... "id":101 ... } ... } ... ); { "acknowledged" : ... Read More
278 Views
To update all the values, use update() along with multi:true. Let us create a collection with documents −> db.demo720.insertOne({"SubjectName":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOne({"SubjectName":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7ce43417811278f5884") } > db.demo720.insertOne({"SubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7d143417811278f5885") } > db.demo720.insertOne({"SubjectName":"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7d543417811278f5886") }Display all documents from a collection with the help of find() method −> db.demo720.find();This will produce the following output −{ "_id" : ObjectId("5eaae7ca43417811278f5883"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eaae7ce43417811278f5884"), "SubjectName" : "Java" } { ... Read More
MongoDB query to add a new field and concatenate the price result divided by a specific number in it
444 Views
To add a new field, use the $addFields in MongoDB. Let us create a collection with documents −> db.demo719.insertOne( ... { ... "Number":"7374644", ... "details" : { ... "otherDetails" : [ ... { ... "ProductId" :"102", ... "ProductPrice" : NumberInt(500) ... }, ... { ... "ProductId" :"103", ... ... Read More
676 Views
The task is to convert the distance from kilometers to meters and centimeters in PL/SQL.PL/SQL is the extension of SQL which combines the Data manipulation of SQL with the working of procedural language.According to the problem we should have distance in kilometers whose value we have to convert in meters and centimeters.As per the conversion rule −1km = 1000 meters1km = 100000 centimetersAccording to this conversion rule we want the distance to be converted by a logic in PL/SQL.ExampleInput: kilometer = 10 Output: meter = 10000 Centimeter = 1000000 Input: kilometer = 9 Output: meter = 9000 Centimeter ... Read More
403 Views
To display only unique records, use distinct() in MongoDB. Let us create a collection with documents −> db.demo613.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988bd4f6b89257f5584d88") } > db.demo613.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988bdbf6b89257f5584d89") } > db.demo613.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988bddf6b89257f5584d8a") } > db.demo613.insertOne({"Name":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988be0f6b89257f5584d8b") } > db.demo613.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988be2f6b89257f5584d8c") } > db.demo613.insertOne({"Name":"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e988be8f6b89257f5584d8d") }Display all documents from a collection with the help of find() method −> db.demo613.find();This will produce the following output −{ "_id" : ObjectId("5e988bd4f6b89257f5584d88"), "Name" : ... Read More
298 Views
Check for a specific field using MongoDB $exists. If that field does not exist in a document, then you need to display the same document with find().Let us create a collection with documents −> db.demo612.insertOne({id:1, "Info":[{Name:"Chris", Age:21}, {Name:"David"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e987372f6b89257f5584d87") }Display all documents from a collection with the help of find() method −> db.demo612.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e987372f6b89257f5584d87"), "id" : 1, "Info" : [ { "Name" : "Chris", "Age" : 21 }, ... Read More
169 Views
To sort documents in MongoDB 4, use sort(). To show only a single field, which is sorted, set it to 1.Let us create a collection with documents −> db.demo611.insertOne({"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987110f6b89257f5584d83") } > db.demo611.insertOne({"Name":"Adam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987115f6b89257f5584d84") } > db.demo611.insertOne({"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e987118f6b89257f5584d85") } > db.demo611.insertOne({"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e98711bf6b89257f5584d86") }Display all documents from a collection with the help of find() method −> db.demo611.find(); This will produce the following output: { "_id" : ObjectId("5e987110f6b89257f5584d83"), "Name" : "Chris" } { "_id" : ObjectId("5e987115f6b89257f5584d84"), "Name" : ... Read More
241 Views
To add together a subset of elements of an array, use $first along with $sum. Let us create a collection with documents −> db.demo610.insertOne({Values:[10, 20, 30, 40, 50]});{ "acknowledged" : true, "insertedId" : ObjectId("5e9747b8f57d0dc0b182d62e") }Display all documents from a collection with the help of find() method −> db.demo610.find().pretty()This will produce the following output −{ "_id" : ObjectId("5e9747b8f57d0dc0b182d62e"), "Values" : [ 10, 20, 30, 40, 50 ] }Here is the query to add together a subset of elements of an array in ... Read More