Found 1659 Articles for Big Data Analytics

Find the count of users who logged in between specific dates with MongoDB

Smita Kapse
Updated on 30-Jul-2019 22:30:26

254 Views

Let’s say you have saved the Login date of users. Now, you want the users who logged in between specific dates i.e. login date. For this, use $gte and $lt operator along with count(). Let us first create a collection with documents −> db.findDataByDateDemo.insertOne({"UserName":"John", "UserLoginDate":new ISODate("2019-01-31")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd8cd7bf3115999ed511ed") } > db.findDataByDateDemo.insertOne({"UserName":"Larry", "UserLoginDate":new ISODate("2019-02-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd8ce7bf3115999ed511ee") } > db.findDataByDateDemo.insertOne({"UserName":"Sam", "UserLoginDate":new ISODate("2019-05-02")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd8cf3bf3115999ed511ef") } > db.findDataByDateDemo.insertOne({"UserName":"David", "UserLoginDate":new ISODate("2019-05-16")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd8d00bf3115999ed511f0") } > db.findDataByDateDemo.insertOne({"UserName":"Carol", ... Read More

Calculate the average value in a MongoDB document grouping by null?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

273 Views

You can use $group operator with _id: null. Following is the syntax −db.yourCollectionName.aggregate([{$group: {_id:null, "anyFieldName": {$avg:"$yourFieldName"} } }]);Let us first create a collection with documents −> db.caculateTheAverageValueDemo.insertOne({"Population":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd68a197924bb85b3f4895f") } > db.caculateTheAverageValueDemo.insertOne({"Population":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd68a1c7924bb85b3f48960") } > db.caculateTheAverageValueDemo.insertOne({"Population":200}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd68a237924bb85b3f48961") } > db.caculateTheAverageValueDemo.insertOne({"Population":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd68a297924bb85b3f48962") } > db.caculateTheAverageValueDemo.insertOne({"Population":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd68a2e7924bb85b3f48963") }Following is the query to display all documents from a collection with the help of find() ... Read More

Implement MongoDB $concatArrays even when some of the values are null?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

222 Views

Use aggregate framework along with $ifNull operator for this. The $concatArrays in aggregation is used to concatenate arrays. Let us first create a collection with documents −>db.concatenateArraysDemo.insertOne({"FirstSemesterSubjects": ["MongoDB", "MySQL", "Java"], "SecondSemesterSubjects":["C", "C++", ]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd687707924bb85b3f4895c") } > db.concatenateArraysDemo.insertOne({"FirstSemesterSubjects":["C#", "Ruby", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd687927924bb85b3f4895d") } >db.concatenateArraysDemo.insertOne({"FirstSemesterSubjects":["HTML", "CSS", "Javascript"], "SecondSemesterSubjects":["CSS", "Javascript"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd687bb7924bb85b3f4895e") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateArraysDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd687707924bb85b3f4895c"), ... Read More

MongoDB query check if value in array property?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

2K+ Views

You can use $in operator to check if a value is in an array or not. Let us first create a collection with documents −> db.valueInArrayDemo.insertOne({"UserName":"John", "UserMessage":["Hi", "Hello", "Bye"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd684cf7924bb85b3f48959") } > db.valueInArrayDemo.insertOne({"UserName":"Larry", "UserMessage":["Thank You", "Amazing", "Nice"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd684d27924bb85b3f4895a") } >db.valueInArrayDemo.insertOne({"UserName":"Carol", "UserMessage":["Awesome", "Bye", "Cool"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd684d87924bb85b3f4895b") }Following is the query to display all documents from a collection with the help of find() method −> db.valueInArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd684cf7924bb85b3f48959"),    "UserName" : "John",    "UserMessage" : [       "Hi",       "Hello",     ... Read More

How do I push elements to an existing array in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

289 Views

To push elements to an existing array, use $addToSet operator along with update(). Let us first create a collection with documents −> db.pushElements.insertOne({"Comments":["Good", "Awesome", "Nice"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd682597924bb85b3f48953") }Following is the query to display all documents from a collection with the help of find() method −> db.pushElements.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd682597924bb85b3f48953"),    "Comments" : [       "Good",       "Awesome",       "Nice"    ] }Following is the query to push elements to an existing array in MongoDB −> db.pushElements.update(    {_id:ObjectId("5cd682597924bb85b3f48953")},    { ... Read More

How to print document value in MongoDB shell?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

For this, work with the concept of forEach(). Let us first create a collection with documents −> db.printDocuementValueDemo.insertOne({"InstructorName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6804f7924bb85b3f48950") } > db.printDocuementValueDemo.insertOne({"InstructorName":"Sam Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd680577924bb85b3f48951") } > db.printDocuementValueDemo.insertOne({"InstructorName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd680637924bb85b3f48952") }Following is the query to display all documents from a collection with the help of find() method −> db.printDocuementValueDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6804f7924bb85b3f48950"),    "InstructorName" : "John Smith" } {    "_id" : ObjectId("5cd680577924bb85b3f48951"),    "InstructorName" : "Sam Williams" } ... Read More

How to remove white spaces (leading and trailing) from string value in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

429 Views

For this, you need to write some code using forEach(). Let us first create a collection with documents −> db.removingWhiteSpaceDemo.insertOne({"Title":" Introduction to java "}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd66f387924bb85b3f4894c") }Following is the query to display all documents from a collection with the help of find() method −> db.removingWhiteSpaceDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd66f387924bb85b3f4894c"), "Title" : " Introduction to java " }Following is the query to remove white spaces (leading and trailing) from string value −> db.removingWhiteSpaceDemo.find({}, {"Title": 1 }).forEach(function(myDocument) {    myDocument.Title = myDocument.Title.trim();    db.removingWhiteSpaceDemo.update(       { "_id": myDocument._id ... Read More

How to add a field with static value to MongoDB find query?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

466 Views

You can use $literal operator along with aggregate framework. Let us first create a collection with documents −> db.fieldWithStaticValue.insertOne({"Name":"Larry", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6554c7924bb85b3f48948") } > db.fieldWithStaticValue.insertOne({"Name":"Chris", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd655567924bb85b3f48949") } > db.fieldWithStaticValue.insertOne({"Name":"David", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd655607924bb85b3f4894a") }Following is the query to display all documents from a collection with the help of find() method −> db.fieldWithStaticValue.find();This will produce the following output −{ "_id" : ObjectId("5cd6554c7924bb85b3f48948"), "Name" : "Larry", "Age" : 24 } { "_id" : ObjectId("5cd655567924bb85b3f48949"), "Name" : "Chris", "Age" : 23 ... Read More

How to get tag count in MongoDB query results based on list of names?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

107 Views

You can use $in operator. Let us first create a collection with documents −> db.tagCountDemo.insertOne({"ListOfNames":["John", "Sam", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b387924bb85b3f48944") } > db.tagCountDemo.insertOne({"ListOfNames":["Bob", "David", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b4b7924bb85b3f48945") } > db.tagCountDemo.insertOne({"ListOfNames":["Mike", "Robert", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b5d7924bb85b3f48946") } > db.tagCountDemo.insertOne({"ListOfNames":["James", "Carol", "Jace"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b717924bb85b3f48947") }Following is the query to display all documents from a collection with the help of find() method −> db.tagCountDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd64b387924bb85b3f48944"),    "ListOfNames" : ... Read More

MongoDB to sort by subdocument match?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

251 Views

To sort by sub-document match, you can use aggregate framework. Let us first create a collection with documents −> db.sortBySubDocumentsDemo.insertOne(    {       "StudentName": "Chris",       "StudentDetails": [          {             "Age":21,             "StudentScore":91          },          {             "Age":22,             "StudentScore":99          },          {             "Age":21,             "StudentScore":93   ... Read More

Advertisements