AmitDiwan has Published 10744 Articles

How to get latest set of data from a MongoDB collection based on the date records?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:16:22

2K+ Views

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, ... Read More

MongoDB Query to implement $in in array

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:13:01

111 Views

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 ... Read More

Text search in MongoDB with Regular Expression

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:10:44

241 Views

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" : ... Read More

How to delete document by _id using MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:09:14

13K+ Views

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" ... Read More

How can I change the field name in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:08:15

225 Views

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") ... Read More

Update the last row with search criteria in MongoDB?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:06:42

384 Views

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, ... Read More

Unwind two arrays from MongoDB

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:04:38

2K+ Views

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": [ ...     ... Read More

MongoDB: Find name similar to Regular Expression input?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:02:14

109 Views

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"}});{   ... Read More

Sum the score of duplicate column values in MongoDB documents?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 07:00:34

195 Views

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"});{   ... Read More

MongoDB query to find matching documents given an array with values?

AmitDiwan

AmitDiwan

Updated on 13-May-2020 06:56:14

209 Views

For specific documents, use MongoDB $in. Let us create a collection with documents −> db.demo511.insertOne({"ListOfProject":["Library Management System", "Hospital Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e88473a987b6e0e9d18f585") } > db.demo511.insertOne({"ListOfProject":["Online Web Tracking", "Library Management System"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e884751987b6e0e9d18f586") } > db.demo511.insertOne({"ListOfProject":["Online Shopping ... Read More

Advertisements