Found 1659 Articles for Big Data Analytics

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

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

332 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

50 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

296 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

Is there a way to limit the number of records in a certain MongoDB collection?

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

80 Views

Yes, you can use capped parameter along with max size. Following is the syntax −db.createCollection("yourCollectionName", {capped:true, size:yourSizeInBytes, max:howManyRecordsYouWant})Let us first create a collection with capped:true −> db.createCollection("limitTheNumberOfRecordsDemo", {capped:true, size:200024, max:3}) { "ok" : 1 }We will now create a collection with documents −> db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"James Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e601b50a6c6dd317adad") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Sam Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e60bb50a6c6dd317adae") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e612b50a6c6dd317adaf") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Carol Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e61ab50a6c6dd317adb0") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Adam Smith"}); {    "acknowledged" ... Read More

Particular field as result in MongoDB?

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

92 Views

To get particular field as a result in MongoDB, you can use findOne(). Following is the syntax −db.yourCollectionName.findOne({"yourFieldName1":yourValue}, {yourFieldName2:1});Let us first create a collection with documents −> db.particularFieldDemo.insertOne({"EmployeeName":"John Smith", "EmployeeAge":26, "EmployeeTechnology":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9b4abb50a6c6dd317ada2") } > db.particularFieldDemo.insertOne({"EmployeeName":"Chris Brown", "EmployeeAge":28, "EmployeeTechnology":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9b4d2b50a6c6dd317ada3") } > db.particularFieldDemo.insertOne({"EmployeeName":"David Miller", "EmployeeAge":30, "EmployeeTechnology":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9b4e8b50a6c6dd317ada4") } > db.particularFieldDemo.insertOne({"EmployeeName":"John Doe", "EmployeeAge":31, "EmployeeTechnology":"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9b527b50a6c6dd317ada5") }Following is the query to display all documents from a collection with the help of ... Read More

Implement MongoDB toLowerCase() in a forEach loop to update the name of students?

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

139 Views

Let us first create a collection with documents wherein one of the fields is StudentName −> db.lowerCaseDemo.insertOne({"StudentName":"JOHN SMith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a86fb50a6c6dd317ad9f") } > db.lowerCaseDemo.insertOne({"StudentName":"CAROL TAYLor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a88fb50a6c6dd317ada0") } > db.lowerCaseDemo.insertOne({"StudentName":"DAVID Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a89fb50a6c6dd317ada1") }Following is the query to display all documents from a collection with the help of find() method −> db.lowerCaseDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd9a86fb50a6c6dd317ad9f"),    "StudentName" : "JOHN SMith" } {    "_id" : ObjectId("5cd9a88fb50a6c6dd317ada0"),    "StudentName" : "CAROL TAYLor" } { ... Read More

MongoDB find() to operate on recursive search?

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

656 Views

Use find() with dot notation to perform recursive search. Let us first create a collection with documents −> db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":101, "ClientName":"Chris"}, {"ClientId":102, "ClientName":"Robert"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a118b50a6c6dd317ad99") } > db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":110, "ClientName":"David"}, {"ClientId":112, "ClientName":"Mike"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9a12fb50a6c6dd317ad9a") }Following is the query to display all documents from a collection with the help of find() method −> db.findOperationDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd9a118b50a6c6dd317ad99"),    "ClientDetails" : [       {          "ClientId" : 101,          "ClientName" : "Chris"       ... Read More

I want to create a new field in an already created document. How can this be done using MongoDB query?

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

53 Views

Use $addToSet to create a new field in MongoDB. Let us first create a collection with documents −> db.createFieldDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99e28b50a6c6dd317ad95") } > db.createFieldDemo.insertOne({"StudentFirstName":"Larry", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99e2fb50a6c6dd317ad96") } > db.createFieldDemo.insertOne({"StudentFirstName":"Chris", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99e38b50a6c6dd317ad97") } > db.createFieldDemo.insertOne({"StudentFirstName":"David", "StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99e43b50a6c6dd317ad98") }Following is the query to display all documents from a collection with the help of find() method −> db.createFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd99e28b50a6c6dd317ad95"),    "StudentFirstName" : ... Read More

How to store MongoDB result in an array?

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

1K+ Views

To store MongoDB result in an array, use the toArray() method −var anyVariableName=db.yourCollectionName.find().toArray();Let us first create a collection with documents −> db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"David Miller", "CustomerAge":24, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99bd5b50a6c6dd317ad92") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Sam Williams", "CustomerAge":46, "isMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99beab50a6c6dd317ad93") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Carol Taylor", "CustomerAge":23, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd99bf9b50a6c6dd317ad94") }Following is the query to display all documents from a collection with the help of find() method −> db.mongoDbResultInArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd99bd5b50a6c6dd317ad92"),    "CustomerName" : "David Miller",    "CustomerAge" ... Read More

Advertisements