AmitDiwan has Published 8358 Articles

How to print NumberLong value in MongoDB?

AmitDiwan

AmitDiwan

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

734 Views

The mongo shell provides the NumberLong() wrapper to handle 64-bit integers. Following is the syntax using custom variable and print using toString() −var anyVariableName=NumberLong("yourLongNumber"); yourVariableName.toString();To understand the above concept, let us implement the above syntax −> var number=NumberLong("231231231231121231"); > number.toString();This will produce the following output −NumberLong("231231231231121231")The second example is as ... Read More

Need to aggregate by hour and $avg in MongoDB

AmitDiwan

AmitDiwan

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

542 Views

To aggregate, use aggregate() in MongoDB. It calculates aggregate values for the data in a collection.Let us create a collection with documents −> db.demo544.insertOne({"DueTime":new ISODate("2020-01-10 12:10:20"), Amount:100});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8e1f029e5f92834d7f05ce") } > db.demo544.insertOne({"DueTime":new ISODate("2020-01-12 12:00:00"), Amount:500});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8e1f089e5f92834d7f05cf") } ... Read More

Find document that matches same array elements in MongoDB?

AmitDiwan

AmitDiwan

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

439 Views

To find a document that matches the same array elements, use find() and within that, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements.Let us create a collection with documents −> db.demo543.insertOne({id:101, subject:["MySQL", "Java" ,"C", "Python"]});{ ... Read More

Add a boolean field true to returned objects, when a specified value is in array. For NULLor other values, set false.

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:54:08

270 Views

For this, use $ifNull. It evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value. Let us first create a collection with documents −> db.demo542.insertOne({"ListOfName":["Chris", "David"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8cabc6ef4dcbee04fbbc17") } > db.demo542.insertOne({"ListOfName":null});{    "acknowledged" : true,   ... Read More

Select documents grouped by field in MongoDB?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:46:37

498 Views

To select documents grouped by field in MongoDB, use $group along with $project. Let us first create a collection with documents −> db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8ca368ef4dcbee04fbbc0e") } > db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e8ca36bef4dcbee04fbbc0f") } > db.demo540.insertOne({id:1, "Name":"Chris", "CountryName":"AUS"});{   ... Read More

MongoDB query to remove subdocument from document?

AmitDiwan

AmitDiwan

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

972 Views

To remove subdocument from a document, use $pull along with update(). Let us first create a collection with documents −> db.demo538.insertOne( ... { ...    id:101, ...    "details": ...    { ...       anotherDetails: ...       [ ...          { ...   ... Read More

Group with multiple fields and get the count of duplicate field values grouped together inMongoDB

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:36:58

1K+ Views

For this, use MongoDB aggregate and within that, use $cond. The $cond evaluates a boolean expression to return one of the two specified return expressions.Let us first create a collection with documents −> db.demo536.insertOne({"Name1":"Chris", "Name2":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8c843eef4dcbee04fbbc01") } > db.demo536.insertOne({"Name1":"David", "Name2":"Chris"});{    "acknowledged" : ... Read More

How do I delete array value from a document in MongoDB?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:33:19

533 Views

To delete array values, use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.Let us first create a collection with documents −> db.demo535.insertOne( ... { ... ...    "studentId" : "101", ...    "studentName" : "Chris", ... Read More

“Structured” grouping query in MongoDB to display result with a new field displaying the count

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:29:48

171 Views

For this, use $group in MongoDB IN aggregate(). The $group groups input documents by the specified _id expression and for each distinct grouping, outputs a document. Let us first create a collection with documents −> db.demo534.insertOne({_id:10, "ProductId":100, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 10 } > db.demo534.insertOne({_id:11, "ProductId":100, "ProductName":"Product-2"}); ... Read More

Unable to implement $addToSet in MongoDB to fetch values of a single field?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 06:25:20

305 Views

The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4cfaef4dcbee04fbbbfc") } > db.demo533.insertOne({"ProjectName":"Online Library Management"});{    "acknowledged" : true, ... Read More

Advertisements