Found 1659 Articles for Big Data Analytics

MongoDB query to select one field if the other is null?

AmitDiwan
Updated on 27-Mar-2020 07:58:40

1K+ Views

To select one field if the other is null, use $ifNull. Let us create a collection with documents −> db.demo182.insertOne({"FirstName":"Chris", "LastName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398ea19e4f06af55199802") } > db.demo182.insertOne({"FirstName":null, "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398ead9e4f06af55199803") } > > db.demo182.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398ebf9e4f06af55199804") }Display all documents from a collection with the help of find() method −> db.demo182.find();This will produce the following output −{ "_id" : ObjectId("5e398ea19e4f06af55199802"), "FirstName" : "Chris", "LastName" : null } { "_id" : ObjectId("5e398ead9e4f06af55199803"), "FirstName" : null, "LastName" : "Miller" } { "_id" ... Read More

MongoDB query to search date records using only Month and Day

AmitDiwan
Updated on 27-Mar-2020 07:56:16

631 Views

To search using month and day only, use $where. Let us create a collection with documents −> db.demo181.insertOne({"ShippingDate":new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a699e4f06af551997fe") } > db.demo181.insertOne({"ShippingDate":new ISODate("2019-12-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a729e4f06af551997ff") } > db.demo181.insertOne({"ShippingDate":new ISODate("2018-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a7d9e4f06af55199800") } > db.demo181.insertOne({"ShippingDate":new ISODate("2020-10-12")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e398a879e4f06af55199801") }Display all documents from a collection with the help of find() method −> db.demo181.find();This will produce the following output −{ "_id" : ObjectId("5e398a699e4f06af551997fe"), "ShippingDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e398a729e4f06af551997ff"), "ShippingDate" : ... Read More

Match MongoDB documents with fields not containing values in array?

AmitDiwan
Updated on 27-Mar-2020 07:53:35

240 Views

To match documents with fields not containing values in array, use $nin. Let us create a collection with documents −> db.demo180.insertOne({"Scores":["80", "90", "110"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3988a69e4f06af551997fb") } > db.demo180.insertOne({"Scores":["110", "70", "60"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3988b79e4f06af551997fc") } > db.demo180.insertOne({"Scores":["40", "70", "1010"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3988cc9e4f06af551997fd") }Display all documents from a collection with the help of find() method −> db.demo180.find();This will produce the following output −{ "_id" : ObjectId("5e3988a69e4f06af551997fb"), "Scores" : [ "80", "90", "110" ] } { "_id" : ObjectId("5e3988b79e4f06af551997fc"), "Scores" : [ "110", "70", ... Read More

How to find latest entries in array over all MongoDB documents?

AmitDiwan
Updated on 27-Mar-2020 07:49:57

72 Views

To find latest entries in array over all document, use aggregate(). Let us create a collection with documents −> db.demo179.insertOne( ...{ ...   "Name":"Chris", ...   "Details": [ ...   { ...      "Id":101, ...      "Subject":"MongoDB" ...   }, ...   { ...      "Id":102, ...      "Subject":"MySQL" ...   } ...   ] ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3980299e4f06af551997f9") } > db.demo179.insertOne( ...{ ...   "Name":"David", ...   "Details": [ ...   { ...      "Id":103, ...      "Subject":"Java" ...   }, ...   { ...   ... Read More

MongoDB query to fetch date records (ISODate format) in a range

AmitDiwan
Updated on 27-Mar-2020 07:40:40

267 Views

Let us create a collection with documents −> db.demo178.insertOne({"DueDate":new ISODate("2019-01-10T06:18:20.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397bd89e4f06af551997f5") } > db.demo178.insertOne({"DueDate":new ISODate("2020-11-10T18:05:11.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397bf39e4f06af551997f6") } > db.demo178.insertOne({"DueDate":new ISODate("2020-03-15T07:05:10.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397c039e4f06af551997f7") } > db.demo178.insertOne({"DueDate":new ISODate("2020-06-11T16:05:10.474Z")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e397c0f9e4f06af551997f8") }Display all documents from a collection with the help of find() method −> db.demo178.find();This will produce the following output −{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") } { "_id" : ObjectId("5e397bf39e4f06af551997f6"), "DueDate" : ISODate("2020-11-10T18:05:11.474Z") } { "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") ... Read More

MongoDB query to add a document in an already created collection

AmitDiwan
Updated on 27-Mar-2020 07:36:36

119 Views

To add a document in an already created collection, use $push in MongoDB. Let us create a collection with documents −> db.demo177.insertOne(    { "Id": "101", "details": [         { "StudentName": "Chris",  "Scores": [67, 71, 74], "SubjectName": ["MySQL", "Java"]  },       { "StudentName": "David", "Scores": [89, 98, 45], "SubjectName": ["PL/SQL", "C"] } ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e384b2b9e4f06af551997f4") }Display all documents from a collection with the help of find() method −> db.demo177.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e384b2b9e4f06af551997f4"),    "Id" : "101",    "details" ... Read More

Get substring in MongoDB aggregate

AmitDiwan
Updated on 27-Mar-2020 07:27:23

502 Views

To get substring, use $substr in MongoDB. Let us create a collection with documents −> db.demo176.insertOne({"ProductName":"PRODUCT-1"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a09e4f06af551997ef") } > db.demo176.insertOne({"ProductName":"PRODUCT-102"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a69e4f06af551997f0") } > db.demo176.insertOne({"ProductName":"PRODUCT-105"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843aa9e4f06af551997f1") }Display all documents from a collection with the help of find() method −> db.demo176.find();This will produce the following output −{ "_id" : ObjectId("5e3843a09e4f06af551997ef"), "ProductName" : "PRODUCT-1" } { "_id" : ObjectId("5e3843a69e4f06af551997f0"), "ProductName" : "PRODUCT-102" } { "_id" : ObjectId("5e3843aa9e4f06af551997f1"), "ProductName" : "PRODUCT-105" }Following is the query to get substring in MongoDB ... Read More

How do I use MongoDB to count only collections that match two fields?

AmitDiwan
Updated on 27-Mar-2020 06:52:51

78 Views

To count only collections that match two fields, use count(). Let us create a collection with documents −> db.demo175.insertOne({"EmployeeName":"Bob", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840969e4f06af551997e8") } > db.demo175.insertOne({"EmployeeName":"David", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e38409e9e4f06af551997e9") } > db.demo175.insertOne({"EmployeeName":"Mike", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840a79e4f06af551997ea") } > db.demo175.insertOne({"EmployeeName":"Sam", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840ae9e4f06af551997eb") }Display all documents from a collection with the help of find() method −> db.demo175.find();This will produce the following output −{ "_id" : ObjectId("5e3840969e4f06af551997e8"), "EmployeeName" : "Bob", "isMarried" : "YES" } { "_id" ... Read More

Evaluate one of more values from a MongoDB collection with documents

AmitDiwan
Updated on 27-Mar-2020 06:51:19

75 Views

To evaluate one or more values, use $or along with find(). Let us create a collection with documents −> db.demo174.insertOne({"StudentName":"Chris", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c709e4f06af551997e5") } > db.demo174.insertOne({"StudentName":"David", "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c779e4f06af551997e6") } > db.demo174.insertOne({"StudentName":"Bob", "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c7e9e4f06af551997e7") }Display all documents from a collection with the help of find() method −> db.demo174.find();This will produce the following output −{ "_id" : ObjectId("5e383c709e4f06af551997e5"), "StudentName" : "Chris", "CountryName" : "US" } { "_id" : ObjectId("5e383c779e4f06af551997e6"), "StudentName" : "David", "CountryName" : "UK" } { "_id" : ... Read More

Limit number of values in a field with MongoDB?

AmitDiwan
Updated on 27-Mar-2020 06:49:39

127 Views

To limit the number of values in a field, use MongoDB $slice. Let us create a collection with documents −> db.demo173.insertOne({"ListOfValues":[10, 40, 100, 560, 700, 900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383a4f9e4f06af551997e4") }Display all documents from a collection with the help of find() method −> db.demo173.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e383a4f9e4f06af551997e4"),    "ListOfValues" : [10, 40, 100, 560, 700, 900 ] }Following is the query to limit number of values in a field using MongoDB −> db.demo173.find({}, { "ListOfValues": { "$slice": -2 } } );This will produce the following output −{ "_id" ... Read More

Advertisements