Found 1349 Articles for MongoDB

Matching MongoDB collection items by id?

AmitDiwan
Updated on 13-May-2020 07:38:24

369 Views

To match collection items by id, use $in in MongoDB. Let us create a collection with documents −> db.demo528.insertOne({"Name":"Chris", Age:21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b00d2ef4dcbee04fbbbe0") } > db.demo528.insertOne({"Name":"Bob", Age:22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b00d9ef4dcbee04fbbbe1") } > db.demo528.insertOne({"Name":"David", Age:20});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b00e0ef4dcbee04fbbbe2") }Display all documents from a collection with the help of find() method −> db.demo528.find();This will produce the following output −{ "_id" : ObjectId("5e8b00d2ef4dcbee04fbbbe0"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e8b00d9ef4dcbee04fbbbe1"), "Name" : "Bob", "Age" : 22 } { "_id" : ObjectId("5e8b00e0ef4dcbee04fbbbe2"), "Name" : "David", ... Read More

Perform Group in MongoDB and sum the price record of documents

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

54 Views

For this, use $group and within that, we need to work with $sum to add. Let us create a collection with documents −> db.demo527.insertOne({"Price":45.5});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff2fef4dcbee04fbbbdc") } > db.demo527.insertOne({"Price":55.5});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff34ef4dcbee04fbbbdd") } > db.demo527.insertOne({"Price":100.4});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff39ef4dcbee04fbbbde") } > db.demo527.insertOne({"Price":200.6});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8aff40ef4dcbee04fbbbdf") }Display all documents from a collection with the help of find() method −> db.demo527.find();This will produce the following output −{ "_id" : ObjectId("5e8aff2fef4dcbee04fbbbdc"), "Price" : 45.5 } { "_id" : ObjectId("5e8aff34ef4dcbee04fbbbdd"), "Price" : 55.5 } ... Read More

Updating an array with $push in MongoDB

AmitDiwan
Updated on 13-May-2020 07:35:21

378 Views

To update an array with $push, use updateOne() in MongoDB. Let us create a collection with documents −> db.demo526.insertOne( ... { ... ...    "CountryName": "US", ...    "TeacherName": "Bob", ...    "StudentInformation": [ ...       { ...          "Name": "Chris", ...          "Subject": "MySQL", ...          "ListOfMailId":[] ...       }, ...       { ...          "Name": "David", ...          "Subject": "MongoDB", ...          "ListOfMailId":[] ... ...       } ...    ] ... } ... Read More

MongoDB query to find multiple matchings inside array of objects?

AmitDiwan
Updated on 13-May-2020 07:32:37

1K+ Views

For this, use $and along with $regex. The $and performs a logical AND operation on an array of one or more expressions and selects the documents that satisfy all the expressions in the array.Let us create a collection with documents −> db.demo525.insertOne({"details":[{Name:"Chris", "CountryName":"US"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae5ef437efc8605595b66") } > db.demo525.insertOne({"details":[{Name:"Bob", "CountryName":"UK"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae5f8437efc8605595b67") } > db.demo525.insertOne({"details":[{Name:"chris", "CountryName":"us"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae602437efc8605595b68") } > db.demo525.insertOne({"details":[{Name:"Mike", "CountryName":"UK"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8ae60b437efc8605595b69") }Display all documents from a collection with the help of find() method −> ... Read More

How to search date between two dates in MongoDB?

AmitDiwan
Updated on 13-May-2020 07:30:11

1K+ Views

To search date between two dates in MongoDB, use $gte and $lt. Let us create a collection with documents −> db.demo524.insertOne({"EndDate":new ISODate("2020-01-19")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8adbe5437efc8605595b63") } > db.demo524.insertOne({"EndDate":new ISODate("2020-01-20")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8adbec437efc8605595b64") } > db.demo524.insertOne({"EndDate":new ISODate("2020-12-31")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8adbf3437efc8605595b65") }Display all documents from a collection with the help of find() method −> db.demo524.find();This will produce the following output −{ "_id" : ObjectId("5e8adbe5437efc8605595b63"), "EndDate" : ISODate("2020-01-19T00:00:00Z") } { "_id" : ObjectId("5e8adbec437efc8605595b64"), "EndDate" : ISODate("2020-01-20T00:00:00Z") } { "_id" : ObjectId("5e8adbf3437efc8605595b65"), "EndDate" : ISODate("2020-12-31T00:00:00Z") }Following is the query ... Read More

Why does my MongoDB group query return always 0 in float conversion? How to fix it?

AmitDiwan
Updated on 13-May-2020 07:26:57

101 Views

For float conversion, use parseFloat() in MongoDB. Let us create a collection with documents −> db.demo523.insertOne({"details":{values:"-0.45"}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e89b7efb3fbf26334ef611f") }Display all documents from a collection with the help of find() method −> db.demo523.find();This will produce the following output −{ "_id" : ObjectId("5e89b7efb3fbf26334ef611f"), "details" : { "values" : "-0.45" } }Following is the query that does not result 0 in the conversion of float −>db.getCollection('demo523').find({}).forEach( function(d) ... { d.details.values = parseFloat( d.details.values ) ... db.getCollection('demo523').save(d)} );Display all documents from a collection with the help of find() method −> db.demo523.find();This will produce the following output −{ "_id" ... Read More

Get distinct pair of objects with all subdocuments in MongoDB?

AmitDiwan
Updated on 13-May-2020 07:24:34

294 Views

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

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

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, "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

MongoDB Query to implement $in in array

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

73 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 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

Text search in MongoDB with Regular Expression

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

195 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" : 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

Advertisements