Found 1659 Articles for Big Data Analytics

Match MongoDB documents with field value greater than a specific number and fetch them?

AmitDiwan
Updated on 15-May-2020 09:20:25

411 Views

To match, use $match in MongoDB. For values greater than a specific number, use $gt. Let us create a collection with documents −> db.demo730.insertOne({"Name" : "Chris", "Marks" : 33 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac54cd56e85a39df5f6339") } > db.demo730.insertOne({ "Name" : "David", "Marks" : 89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac54cd56e85a39df5f633a") } > db.demo730.insertOne({ "Name" : "Chris", "Marks" : 45 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac54ce56e85a39df5f633b") }Display all documents from a collection with the help of find() method −> db.demo730.find();This will produce the following output −{ "_id" : ObjectId("5eac54cd56e85a39df5f6339"), "Name" ... Read More

Mass insertion in MongoDB

AmitDiwan
Updated on 15-May-2020 09:18:39

75 Views

For mass insertion, use the concept of insertMany() in MongoDB. The insertMany() inserts multiple documents into a collection.Let us create a collection with documents −> db.demo729.insertMany( [ ...    { BankName:"HDFC Bank", cardType:"Credit", "CustomerName":[{Name:"Chris", Age:25}]}, ...    { BankName:"ICICI Bank", cardType:"Debit", "CustomerName":[{Name:"Bob", Age:22}]}, ...    { BankName:"Kotak Bank", cardType:"Debit", "CustomerName":[{Name:"David", Age:23}]} ... ] ); {    "acknowledged" : true,    "insertedIds" : [       ObjectId("5eac510d56e85a39df5f6333"),       ObjectId("5eac510d56e85a39df5f6334"),       ObjectId("5eac510d56e85a39df5f6335")    ] }Display all documents from a collection with the help of find() method −> db.demo729.find().pretty();This will produce the following output −{    "_id" : ... Read More

Find MongoDB records with Price less than a specific value

AmitDiwan
Updated on 15-May-2020 09:16:20

320 Views

To check the records with Price less than a specific value, use $lt. Let us create a collection with documents −> db.demo728.insertOne({Price:75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab413c43417811278f589b") } > db.demo728.insertOne({Price:59}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab414043417811278f589c") } > db.demo728.insertOne({Price:79}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab414543417811278f589d") } > db.demo728.insertOne({Price:89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab414843417811278f589e") }Display all documents from a collection with the help of find() method −> db.demo728.find();This will produce the following output −{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 } { "_id" : ObjectId("5eab414043417811278f589c"), "Price" : ... Read More

MongoDB query to search for string like “@email” in the field values

AmitDiwan
Updated on 15-May-2020 09:14:43

1K+ Views

Search for email string using MongoDB find(). Let us create a collection with documents −> db.demo727.insertOne({UserId:"John@email.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab375f43417811278f5898") } > db.demo727.insertOne({UserId:"John@yahoo.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab376043417811278f5899") } > db.demo727.insertOne({UserId:"Chris@EMAIL.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab376143417811278f589a") }Display all documents from a collection with the help of find() method −> db.demo727.find();This will produce the following output −{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "John@email.com" } { "_id" : ObjectId("5eab376043417811278f5899"), "UserId" : "John@yahoo.com" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "Chris@EMAIL.com" }Following is the query to search for @email like ... Read More

How can I count the documents in an array based on the value of a specific field?

AmitDiwan
Updated on 15-May-2020 09:12:54

106 Views

For such match and count, use $match in MongoDB. Let us create a collection with documents −> db.demo726.insertOne( ...    { ...       id:101, ...       "details": [ ...          { ...             Name:"Chris" ... ...          }, ...          { ...             Name:"Chris" ... ...          }, ...          { ...             Name:"Bob" ...          } ...       ] ... ... Read More

Set filtering conditions for nested array in MongoDB

AmitDiwan
Updated on 15-May-2020 09:10:01

1K+ Views

To set filtering conditions, use $filter and $cond in MongoDB aggregate(). The $filter selects a subset of an array to return based on the specified condition. Let us create a collection with documents −> db.demo725.insertOne( ...    { ... ...       "details": { ... ...          "userMessages": [ ...             { ...                "Messages": [ ...                   { "Message": "Hello" }, ...                   { "Message": "How" }, ... ... Read More

How to count a cursor’s iteration in MongoDB?

AmitDiwan
Updated on 15-May-2020 09:05:42

145 Views

You need to use custom logic with the help of while loop along with find() cursor. Let us create a collection with documents −> db.demo724.insertOne( ...    { ...       details: ...       { ...          id:101, ...          otherDetails:[ ...             {Name:"Chris"} ...          ] ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab0cce43417811278f5890") } > > > db.demo724.insertOne( ... { ... ... } ... ); {    "acknowledged" : true, ... Read More

Get count of array elements from a specific field in MongoDB documents?

AmitDiwan
Updated on 15-May-2020 09:02:54

651 Views

To count array elements from a specific field, use $size in MongoDB. Let us create a collection with documents −> db.demo723.insertOne({"Subject":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab094d43417811278f588a") } > db.demo723.insertOne({"Subject":["C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab095243417811278f588b") } > db.demo723.insertOne({"Subject":["C++", "Java", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab095f43417811278f588c") }Display all documents from a collection with the help of find() method −> db.demo723.find();This will produce the following output −{ "_id" : ObjectId("5eab094d43417811278f588a"), "Subject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5eab095243417811278f588b"), "Subject" : [ "C" ] } { "_id" : ... Read More

Ignore null values in MongoDB documents

AmitDiwan
Updated on 15-May-2020 09:01:54

2K+ Views

To ignore null values in MongoDB, use "$ne" : null in aggregate(). Let us create a collection with documents −> db.demo722.insertOne( ...    { ...       id:101, ...       details: [ ...          { Name:""}, ...          { Name: "David"}, ...          {Name:null}, ...          {Name:"Carol"} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab07d543417811278f5889") }Display all documents from a collection with the help of find() method −> db.demo722.find();This will produce the following output ... Read More

Count by multiple fields with MongoDB aggregation

AmitDiwan
Updated on 15-May-2020 08:59:09

704 Views

To count by multiple fields, use $facet in MongoDB. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Let us create a collection with documents −> db.demo721.insertOne( ...    { ... ...       "details1": { ...          "id":101 ... ...       }, ...       "details2": { ...          "id":101 ...       }, ...       "details3": { ...          "id":101 ...       } ...    } ... ); {    "acknowledged" : ... Read More

Advertisements