To get a distinct pair of objects, use $group. Let us create a collection with documents −> db.demo522.insertOne({"Name":"John", "Score":45});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b646b3fbf26334ef611b") } > db.demo522.insertOne({"Name":"Bob", "Score":67});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b64eb3fbf26334ef611c") } > db.demo522.insertOne({"Name":"John", "Score":55});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b655b3fbf26334ef611d") } > db.demo522.insertOne({"Name":"Bob", "Score":33});{ "acknowledged" : true, "insertedId" : ObjectId("5e89b65cb3fbf26334ef611e") }Display all documents from a collection with the help of find() method −> db.demo522.find();This will produce the following output −{ "_id" : ObjectId("5e89b646b3fbf26334ef611b"), "Name" : "John", "Score" : 45 } { "_id" : ObjectId("5e89b64eb3fbf26334ef611c"), "Name" : "Bob", ... Read More
To get the latest set of data from data records, use sort() and -1. For only a single data i.e. document, use LIMIT(1). Let us create a collection with documents −> db.demo521.insertOne({"PurchaseDate":new ISODate("2019-01-10"), "ProductName":"Product-1"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1acb3fbf26334ef6117") } > db.demo521.insertOne({"PurchaseDate":new ISODate("2020-04-05"), "ProductName":"Product-10"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1b9b3fbf26334ef6118") } > db.demo521.insertOne({"PurchaseDate":new ISODate("2010-05-08"), "ProductName":"Product-4"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1c8b3fbf26334ef6119") } > db.demo521.insertOne({"PurchaseDate":new ISODate("2020-02-21"), "ProductName":"Product-3"});{ "acknowledged" : true, "insertedId" : ObjectId("5e89a1d7b3fbf26334ef611a") }Display all documents from a collection with the help of find() method −> db.demo521.find();This will produce the following output −{ "_id" : ObjectId("5e89a1acb3fbf26334ef6117"), ... Read More
Let us create a collection with documents −> db.demo520.insertOne({"ListOfName":["John", "Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e899fb4b3fbf26334ef6114") } > db.demo520.insertOne({"ListOfName":["Chris", "David"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e899fbfb3fbf26334ef6115") } > db.demo520.insertOne({"ListOfName":["Mike", "Bob"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e899fc7b3fbf26334ef6116") }Display all documents from a collection with the help of find() method −> db.demo520.find();This will produce the following output −{ "_id" : ObjectId("5e899fb4b3fbf26334ef6114"), "ListOfName" : [ "John", "Bob" ] } { "_id" : ObjectId("5e899fbfb3fbf26334ef6115"), "ListOfName" : [ "Chris", "David" ] } { "_id" : ObjectId("5e899fc7b3fbf26334ef6116"), "ListOfName" : [ "Mike", "Bob" ] }Here is the query to implement $in ... Read More
For text search in MongoDB with Regular Expression, use $regex. Let us create a collection with documents −> db.demo519.insertOne({"Value":"50, 60, 70"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b9c0b3fbf26334ef6111") } > db.demo519.insertOne({"Value":"80, 90, 50"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b9c7b3fbf26334ef6112") } > db.demo519.insertOne({"Value":"10, 30, 40"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b9cfb3fbf26334ef6113") }Display all documents from a collection with the help of find() method −> db.demo519.find();This will produce the following output −{ "_id" : ObjectId("5e88b9c0b3fbf26334ef6111"), "Value" : "50, 60, 70" } { "_id" : ObjectId("5e88b9c7b3fbf26334ef6112"), "Value" : "80, 90, 50" } { "_id" : ObjectId("5e88b9cfb3fbf26334ef6113"), "Value" : ... Read More
To delete by _id, use remove() in MongoDB. Following is the syntax −db.yourCollectionName.remove({_id:yourObjectId});To understand the above syntax, let us create a collection with documents −> db.demo518.insertOne({"ClientName":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b02db3fbf26334ef610e") } > db.demo518.insertOne({"ClientName":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b030b3fbf26334ef610f") } > db.demo518.insertOne({"ClientName":"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88b035b3fbf26334ef6110") }Display all documents from a collection with the help of find() method −> db.demo518.find();This will produce the following output −{ "_id" : ObjectId("5e88b02db3fbf26334ef610e"), "ClientName" : "Chris" } { "_id" : ObjectId("5e88b030b3fbf26334ef610f"), "ClientName" : "Bob" } { "_id" : ObjectId("5e88b035b3fbf26334ef6110"), "ClientName" : "David" }Following is ... Read More
To change the field name, use the $project. Let us create a collection with documents −> db.demo517.insertOne({"Name":"Chris Brown"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88a2a2987b6e0e9d18f595") } > db.demo517.insertOne({"Name":"David Miller"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88a2ab987b6e0e9d18f596") } > db.demo517.insertOne({"Name":"John Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88a2b1987b6e0e9d18f597") }Display all documents from a collection with the help of find() method −> db.demo517.find();This will produce the following output −{ "_id" : ObjectId("5e88a2a2987b6e0e9d18f595"), "Name" : "Chris Brown" } { "_id" : ObjectId("5e88a2ab987b6e0e9d18f596"), "Name" : "David Miller" } { "_id" : ObjectId("5e88a2b1987b6e0e9d18f597"), "Name" : "John Doe" }Following is the query to ... Read More
To update with search criteria, use findAndModify() in MongoDB. Let us create a collection with documents −> db.demo516.insertOne({"Name":"John", "Age":22, "Score":56});{ "acknowledged" : true, "insertedId" : ObjectId("5e889fdb987b6e0e9d18f591") } > db.demo516.insertOne({"Name":"John", "Age":23, "Score":67});{ "acknowledged" : true, "insertedId" : ObjectId("5e889ff1987b6e0e9d18f592") } > db.demo516.insertOne({"Name":"John", "Age":22, "Score":56});{ "acknowledged" : true, "insertedId" : ObjectId("5e889ff3987b6e0e9d18f593") } > db.demo516.insertOne({"Name":"John", "Age":22, "Score":66});{ "acknowledged" : true, "insertedId" : ObjectId("5e889ffa987b6e0e9d18f594") }Display all documents from a collection with the help of find() method −> db.demo516.find();This will produce the following output −{ "_id" : ObjectId("5e889fdb987b6e0e9d18f591"), "Name" : "John", "Age" : 22, "Score" : 56 } ... Read More
To unwind, use $unwind. The $unwind deconstructs an array field from the input documents to output a document for each element.Let us create a collection with documents −> db.demo515.insertOne( ... { ... "details1": [ ... "4700100004" ... ], ... "details2": [ ... "Not Given" ... ], ... "Value1": [ ... "56", ... "45", ... "35", ... ], ... "Value2": [ ... "35", ... "45", ... "56", ... ]} ... Read More
Send the name with $regex in MongoDB to find a name similar to the input. Let us create a collection with documents −> db.demo514.insertOne({"Information":{"FullName":"John Doe"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e885116987b6e0e9d18f58c") } > db.demo514.insertOne({"Information":{"FullName":"John Smith"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e88515e987b6e0e9d18f58d") } > db.demo514.insertOne({"Information":{"FullName":"john doe"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e885169987b6e0e9d18f58e") } > db.demo514.insertOne({"Information":{"FullName":"Chris Brown"}});{ "acknowledged" : true, "insertedId" : ObjectId("5e88516f987b6e0e9d18f58f") }Display all documents from a collection with the help of find() method −> db.demo514.find();This will produce the following output −{ "_id" : ObjectId("5e885116987b6e0e9d18f58c"), "Information" : { "FullName" : "John Doe" } ... Read More
To sum values in different documents, use MongoDB $group. Let us create a collection with documents −> db.demo512.insertOne({"Name":"Chris", "Score1":45, "Score2":65, "CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e884d96987b6e0e9d18f588") } > db.demo512.insertOne({"Name":"Chris", "Score1":41, "Score2":45, "CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e884da2987b6e0e9d18f589") } > db.demo512.insertOne({"Name":"Bob", "Score1":75, "Score2":55, "CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e884db2987b6e0e9d18f58a") } > db.demo512.insertOne({"Name":"Bob", "Score1":65, "Score2":90, "CountryName":"US"});{ "acknowledged" : true, "insertedId" : ObjectId("5e884dc2987b6e0e9d18f58b") }Display all documents from a collection with the help of find() method −> db.demo512.find();This will produce the following output −{ "_id" : ObjectId("5e884d96987b6e0e9d18f588"), "Name" : "Chris", "Score1" : 45, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP