Found 6702 Articles for Database

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

184 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

268 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

836 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

332 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

226 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

How do I remove a string from an array in a MongoDB document?

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

333 Views

You can use $pull operator to remove a string from an array. Let us first create a collection with documents −> db.removeAStringDemo.insertOne({"Score":[45, 67, 89, "John", 98, 99, 67]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda5224b50a6c6dd317adbd") }Following is the query to display all documents from a collection with the help of find() method −> db.removeAStringDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cda5224b50a6c6dd317adbd"),    "Score" : [       45,       67,       89,       "John",       98,       99,       67    ] ... Read More

Find a strict document that contains only a specific field with a fixed length?

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

52 Views

You can use $where operator for this. Let us first create a collection with documents −>db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4bcdb50a6c6dd317adb8") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4bdbb50a6c6dd317adb9") } >db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4becb50a6c6dd317adba") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4bfbb50a6c6dd317adbb") } > db.veryStrictDocumentDemo.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda4c6db50a6c6dd317adbc") }Following is the query to display all documents from a collection with the help of find() method −> db.veryStrictDocumentDemo.find();This will produce the following output ... Read More

How to get array from a MongoDB collection?

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

297 Views

You can use aggregate framework. Let us first create a collection with documents −> db.getArrayDemo.insertOne(    {       "CustomerId":101,       "CustomerDetails":[          {             "CustomerName":"Larry",             "CustomerFriendDetails":[                {                   "CustomerFriendName":"Sam"                },                {                   "CustomerFriendName":"Robert"                }         ... Read More

MongoDB query where all array items are greater than a specified condition?

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

459 Views

You can use $gt operator for this. Let us first create a collection with documents −> db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[89, 43, 32, 45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e9f9b50a6c6dd317adb3") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[32, 33, 34, 40]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea13b50a6c6dd317adb4") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[45, 56, 66, 69]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea25b50a6c6dd317adb5") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[46, 66, 77, 88]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea3cb50a6c6dd317adb6") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayElementsNotGreaterThanDemo.find().pretty();This will produce the following output −{   ... Read More

Advertisements