Found 1659 Articles for Big Data Analytics

MongoDB query to find a value from JSON like data?

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

4K+ Views

To fund a value from JSON data, use the find() along with dot(.) notation. Let us first create a collection with documents −> db.findValueFromJsonDemo.insertOne(    {       "UserDetails": [{          "_id": new ObjectId(),          "UserName": "Carol",          "UserMessage": "Hi"       }],       "UserFriendsName": ["John", "Sam"]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdf8a4cbf3115999ed511fd") }Following is the query to display all documents from a collection with the help of find() method −> db.findValueFromJsonDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

MongoDB Limit fields and slice projection together?

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

208 Views

Use the $slice operator. Let us first create a collection with documents −> db.limitAndSliceProjectionDemo.insertOne(    {       "_id" : 101,       "UserName" : "Carol",       "UserAge" : 26,       "UserMesssage" : [          "Hi",          "Hello",          "Bye",          "Awesome",          "Good",          "Bad",          "Nice",          "Good Night",          "Good Morning"       ]    } ); { "acknowledged" : true, "insertedId" : ... Read More

How to add a sub-document to sub-document array in MongoDB?

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

2K+ Views

Use the $push operator to add a sub-document. Let us first create a collection with documents −> db.subDocumentToSubDocumentDemo.insertOne(    {       "_id" :101,       "StudentName" : "Larry",       "StudentAge" : 21,       "StudentDetails" : [          {             "StudentCountryName" : "US",             "StudentFavouriteSubjectList" : [ ]          }       ]    } ); { "acknowledged" : true, "insertedId" : 101 }Following is the query to display all documents from a collection with the help ... Read More

Check if a list is not empty in MongoDB?

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

339 Views

For this, use the $size operator. Let us first create a collection with documents −> db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":["John", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99e8bf3115999ed511f7") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":["Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99e9bf3115999ed511f8") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99ebbf3115999ed511f9") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[null]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99f2bf3115999ed511fa") } > db.checkIfListIsNotEmptyDemo.insertOne({"UserFriendGroup":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd99f6bf3115999ed511fb") }Following is the query to display all documents from a collection with the help of find() method −> db.checkIfListIsNotEmptyDemo.find().pretty();This will produce the following output −{   ... Read More

Find data for specific date in MongoDB?

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

2K+ Views

Let’s say you have saved the Login date of users. Now, you want the count of records for specific date only 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

Efficient way to remove all entries from MongoDB?

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

183 Views

If you will try to use the method drop(), then it will delete all information about the collection. Indexing is fast. However, if you will use the method remove(), then it removes all records but keeps the collection and indexes.Let us check with the help of example.Using drop()Let us first create a collection with documents −> db.dropWorkingDemo.createIndex({"FirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.dropWorkingDemo.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd8742bf3115999ed511e9") }Following is the query to display all documents from a collection with the ... Read More

How to select where sum of fields is greater than a value in MongoDB?

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

265 Views

You can use $where operator for this. Let us first create a collection with documents −> db.sumOfFieldIsGreaterThanDemo.insertOne({"Price1":10, "Price2":50, "Price3":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd84b8bf3115999ed511e6") } > db.sumOfFieldIsGreaterThanDemo.insertOne({"Price1":11, "Price2":1, "Price3":120}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd84c6bf3115999ed511e7") } > db.sumOfFieldIsGreaterThanDemo.insertOne({"Price1":10, "Price2":9, "Price3":6}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdd84d2bf3115999ed511e8") }Following is the query to display all documents from a collection with the help of find() method −> db.sumOfFieldIsGreaterThanDemo.find();This will produce the following output −{ "_id" : ObjectId("5cdd84b8bf3115999ed511e6"), "Price1" : 10, "Price2" : 50, "Price3" : 40 } { "_id" : ObjectId("5cdd84c6bf3115999ed511e7"), "Price1" : ... Read More

How to push new items to an array inside of an object in MongoDB?

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

830 Views

You can use $elemMatch operator for this. Let us first create a collection with documents −> db.pushNewItemsDemo.insertOne(    {       "_id" :1,       "StudentScore" : 56,       "StudentOtherDetails" : [          {             "StudentName" : "John",             "StudentFriendName" : [                "Bob",                "Carol"             ]          },          {             "StudentName" : ... Read More

How to update multiple documents in MongoDB?

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

328 Views

You need to use multi:true to update multiple documents. Let us first create a collection with documents −> db.multiUpdateDemo.insertOne({"ClientName":"John", "ClientAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda5bc0b50a6c6dd317adc8") } > db.multiUpdateDemo.insertOne({"ClientName":"Carol", "ClientAge":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda5bc1b50a6c6dd317adc9") } > db.multiUpdateDemo.insertOne({"ClientName":"John", "ClientAge":39}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda5bc3b50a6c6dd317adca") } > db.multiUpdateDemo.insertOne({"ClientName":"John", "ClientAge":41}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda5bc5b50a6c6dd317adcb") } > db.multiUpdateDemo.insertOne({"ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda5bc6b50a6c6dd317adcc") }Following is the query to display all documents from a collection with the help of find() method −> db.multiUpdateDemo.find().pretty();This ... Read More

Push new key element into subdocument of MongoDB?

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

224 Views

You can use $set operator for this. Following is the syntax −db.yourCollectionName.update({"_id" : yourObjectId }, {$set: { "yourOuterFieldName.anyInnerFieldName": yourValue}});Let us first create a collection with documents −> db.pushNewKeyDemo.insertOne({"UserId":100, "UserDetails":{}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda58f5b50a6c6dd317adbf") }Following is the query to display all documents from a collection with the help of find() method −> db.pushNewKeyDemo.find();This will produce the following output −{ "_id" : ObjectId("5cda58f5b50a6c6dd317adbf"), "UserId" : 100, "UserDetails" : { } }Following is the query to push new key element into subdocument of MongoDB −> db.pushNewKeyDemo.update({"_id" : ObjectId("5cda58f5b50a6c6dd317adbf")}, {$set: {    "UserDetails.UserName": "David Miller"}}); WriteResult({ "nMatched" : 1, ... Read More

Advertisements