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 128 of 618
200 Views
For this, use skip() in MongoDB. Under skip(), set “count() – 10” to get 10 most recent documents. Let us create a collection with documents −> db.demo500.insertOne({value:10});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749c5987b6e0e9d18f55a") } > db.demo500.insertOne({value:1200});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749c8987b6e0e9d18f55b") } > db.demo500.insertOne({value:19});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749cb987b6e0e9d18f55c") } > db.demo500.insertOne({value:28});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749cf987b6e0e9d18f55d") } > db.demo500.insertOne({value:50});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749d1987b6e0e9d18f55e") } > db.demo500.insertOne({value:70});{ "acknowledged" : true, "insertedId" : ObjectId("5e8749d4987b6e0e9d18f55f") } > db.demo500.insertOne({value:100});{ "acknowledged" : true, "insertedId" : ... Read More
470 Views
To query an array of subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo499.insertOne({ ... "details": ... [ ... { ... Name :"MIT", ... Rank: 1, ... "CountryName":"US" ... }, ... ... { ... Name :"Stanford University", ... Rank: 2 ... }, ... ... { ... Name :"University of California, Berkeley", ... ... Read More
1K+ Views
To retrieve group by result with arrays, use aggregate(). We will also use $addToSet operator. It adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo498.insertOne({id:1, Name:["Chris"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e86192b987b6e0e9d18f553") } > db.demo498.insertOne({id:2, Name:["David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e86192d987b6e0e9d18f554") } > db.demo498.insertOne({id:3, Name:["Chris"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861931987b6e0e9d18f555") } > db.demo498.insertOne({id:4, Name:["Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e861942987b6e0e9d18f556") } > db.demo498.insertOne({id:5, Name:["David"]});{ "acknowledged" : true, "insertedId" ... Read More
502 Views
We have the following date −01-10-2019To change the date format, let us use custom variable and convert date to a string and change its format −Following is the query to implement date to string −> var inputDate="01-10-2019"; > var formatDate= inputDate.split(/-|\//); > var outputString= formatDate[2]+'-'+formatDate[0]+'-'+formatDate[1];Displaying the variable value −> print(outputString);This will produce the following output −2019-01-10
617 Views
To find if element exists in a MongoDB document, use MongoDB $exists. Let us create a collection with documents −> db.demo497.insertOne({"details":[{"Name":"Chris"}, {"Name":"Bob"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b3cfb0f3fa88e22790d1") } > db.demo497.insertOne({"details":[{"Name":"Carol"}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b3d9b0f3fa88e22790d2") } > db.demo497.insertOne({"details":[{}]});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b3e9b0f3fa88e22790d3") }Display all documents from a collection with the help of find() method −> db.demo497.find();This will produce the following output −{ "_id" : ObjectId("5e84b3cfb0f3fa88e22790d1"), "details" : [ { "Name" : "Chris" }, { "Name" : "Bob" } ] } { "_id" : ObjectId("5e84b3d9b0f3fa88e22790d2"), "details" : [ { "Name" : ... Read More
347 Views
For this, use the forEach(). To display the values, use printjson(). Let us create a collection with documents −> db.demo496.insertOne({"Name":"David", "CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b04ab0f3fa88e22790ce") } > db.demo496.insertOne({"Name":"John", "CountryName":"AUS"});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b054b0f3fa88e22790cf") } > db.demo496.insertOne({"Name":"Robert", "CountryName":"UK"});{ "acknowledged" : true, "insertedId" : ObjectId("5e84b05db0f3fa88e22790d0") }Display all documents from a collection with the help of find() method −> db.demo496.find();This will produce the following output −{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" } { "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : ... Read More
267 Views
To update only a single document, you need to update a specific data with updateOne(). The updateOne() is used to update a single document within the collection based on the filter.Let us create a collection with documents −> db.demo495.insertOne({"FirstName":"Chris", "Age":19});{ "acknowledged" : true, "insertedId" : ObjectId("5e84adfeb0f3fa88e22790ca") } > db.demo495.insertOne({"FirstName":"David", "Age":21});{ "acknowledged" : true, "insertedId" : ObjectId("5e84ae05b0f3fa88e22790cb") } > db.demo495.insertOne({"FirstName":"Bob", "Age":26});{ "acknowledged" : true, "insertedId" : ObjectId("5e84ae0eb0f3fa88e22790cc") } > db.demo495.insertOne({"FirstName":"John", "Age":22});{ "acknowledged" : true, "insertedId" : ObjectId("5e84ae15b0f3fa88e22790cd") }Display all documents from a collection with the help of find() method −> db.demo495.find();This will produce ... Read More
2K+ Views
To update elements inside an array, use $set in MongoDB. Let us create a collection with documents −> db.demo494.insertOne( ... { ... ... "CollegeDetails" : [ ... { ... "CollegeName" : "MIT", ... "Fees" : 80000 ... }, ... { ... "CollegeName" : "SU", ... "Fees" : 90000 ... } ... ] ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5e84a5c1b0f3fa88e22790c9") }Display all documents from a ... Read More
865 Views
To count unique items in array-based fields, use $group along with aggregate(). Let us create a collection with documents −> db.demo493.insertOne({"SubjectName":["MySQL", "MongoDB", "Java"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e849f97b0f3fa88e22790c4") } > db.demo493.insertOne({"SubjectName":["C++", "MongoDB", "C"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e849fa4b0f3fa88e22790c5") } > db.demo493.insertOne({"SubjectName":["MySQL", "MongoDB", "C"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e849fb2b0f3fa88e22790c6") }Display all documents from a collection with the help of find() method −> db.demo493.find();This will produce the following output −{ "_id" : ObjectId("5e849f97b0f3fa88e22790c4"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e849fa4b0f3fa88e22790c5"), "SubjectName" : [ "C++", "MongoDB", "C" ] } ... Read More
512 Views
For nested queries, let us first create a collection with documents −> db.demo492.insertOne({ ... "ProductDetails" : ... { ... "StockDetails" : [ ... { "ProductName" : "Product-1" }, ... {"ProductName" : "Product-2"}, ... { "ProductName" : "Product-3"} ... ... ] ... ... } ... }); { "acknowledged" : true, "insertedId" : ObjectId("5e849db8b0f3fa88e22790c2") } > > > > db.demo492.insertOne({ ... "ProductDetails" : ... { ... "StockDetails" : [ ... ... Read More