Found 1659 Articles for Big Data Analytics

Using regex in MongoDB findOne()

AmitDiwan
Updated on 13-May-2020 09:12:23

489 Views

The findOne() returns one document that satisfies the specified query criteria on the collection. Let us create a collection with documents −> db.demo655.insertOne({subject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea050254deddd72997713cc") } > db.demo655.insertOne({subject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea0502b4deddd72997713cd") } > db.demo655.insertOne({subject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea050314deddd72997713ce") }Display all documents from a collection with the help of find() method −> db.demo655.find();This will produce the following output −{ "_id" : ObjectId("5ea050254deddd72997713cc"), "subject" : "MySQL" } { "_id" : ObjectId("5ea0502b4deddd72997713cd"), "subject" : "MongoDB" } { "_id" : ObjectId("5ea050314deddd72997713ce"), "subject" : "Java" }Following is ... Read More

In MongoDB, is using $in search faster than multiple single searches?

AmitDiwan
Updated on 13-May-2020 09:09:59

69 Views

Yes, using $in is faster. Let us see an example and create a collection with documents −> db.demo653.insertOne({subject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b274deddd72997713c0") } > db.demo653.insertOne({subject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b304deddd72997713c1") } > db.demo653.insertOne({subject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b354deddd72997713c2") } > db.demo653.insertOne({subject:"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b384deddd72997713c3") } > db.demo653.insertOne({subject:"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea04b3b4deddd72997713c4") }Display all documents from a collection with the help of find() method −> db.demo653.find();This will produce the following output −{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : ... Read More

Sort the MongoDB documents in ascending order with aggregation?

AmitDiwan
Updated on 13-May-2020 09:08:39

148 Views

Use $sort in MongoDB aggregation. Let us create a collection with documents −> db.demo652.insertOne({ ...    value:10, ...    "details" : [{ ...       "ProductName" : "Product-1", ...       "ProductQuantity" : 8, ...       "ProductPrice" : 500 ...    }, { ...          "ProductName" : "Product-2", ...          "ProductQuantity" : 7, ...          "ProductPrice" : 500 ... ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9f0730e3c3cd0dcff36a62") } > > db.demo652.insertOne({ ...    value:5, ...   ... Read More

Avoid MongoDB performance issues while using regex

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

362 Views

To avoid performance issues in MongoDB, use the concept of index. Let us create a collection with documents −> db.demo531.createIndex({"CountryName":"text", "Name":"text"});{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo531.insertOne({CountryName:"US", "Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2b60ef4dcbee04fbbbf2") } > db.demo531.insertOne({CountryName:"UK", "Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2b6cef4dcbee04fbbbf3") } > db.demo531.insertOne({CountryName:"AUS", "Name":"chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2b74ef4dcbee04fbbbf4") } > db.demo531.insertOne({CountryName:"US", "Name":"CHRIS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2badef4dcbee04fbbbf5") }Display all documents from a collection with the help of find() method −> db.demo531.find();This will ... Read More

How to use ORDERBY in MongoDB if there are possible null values?

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

581 Views

If there are null values also, then implement ORDERBY using sort().Note − Since, starting in MongoDB v3.2, the $orderby operator deprecated in the mongo shell. Use cursor.sort() instead.Let us create a collection with documents −> db.demo530.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2990ef4dcbee04fbbbec") } > db.demo530.insertOne({"Name":null});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2991ef4dcbee04fbbbed") } > db.demo530.insertOne({"Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2992ef4dcbee04fbbbee") } > db.demo530.insertOne({"Name":"Adam"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2995ef4dcbee04fbbbef") } > db.demo530.insertOne({"Name":null});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b2999ef4dcbee04fbbbf0") } > db.demo530.insertOne({"Name":"Carol"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b299eef4dcbee04fbbbf1") }Display ... Read More

MongoDB query to group by _id

AmitDiwan
Updated on 13-May-2020 07:41:39

784 Views

To group by _id in MongoDB, use $group. Let us create a collection with documents −> db.demo529.insertOne({"Score":10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d5bef4dcbee04fbbbe4") } > db.demo529.insertOne({"Score":20});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d5fef4dcbee04fbbbe5") } > db.demo529.insertOne({"Score":10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d61ef4dcbee04fbbbe6") } > db.demo529.insertOne({"Score":10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b1d62ef4dcbee04fbbbe7") }Display all documents from a collection with the help of find() method −> db.demo529.find();This will produce the following output −{ "_id" : ObjectId("5e8b1d5bef4dcbee04fbbbe4"), "Score" : 10 } { "_id" : ObjectId("5e8b1d5fef4dcbee04fbbbe5"), "Score" : 20 } { "_id" : ObjectId("5e8b1d61ef4dcbee04fbbbe6"), "Score" : 10 ... Read More

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

Advertisements