Found 1349 Articles for MongoDB

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

Get all fields names in a MongoDB collection?

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

3K+ Views

You can use the concept of Map Reduce. Let us first create a collection with documents −> db.getAllFieldNamesDemo.insertOne({"StudentFirstName":"David", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd998e9b50a6c6dd317ad90") }Following is the query to display all documents from a collection with the help of find() method −> db.getAllFieldNamesDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd998e9b50a6c6dd317ad90"), "StudentFirstName" : "David", "StudentAge" : 23 }Following is the query to get all fields names in a MongoDB collection −> myMapReduce = db.runCommand({    "mapreduce" : "getAllFieldNamesDemo",    "map" : function() {       for (var myKey in this) { emit(myKey, null); } ... Read More

How can I update all elements in an array with a prefix string?

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

383 Views

To update all elements in an array with a prefix string, use forEach(). Let us first create a collection with documents −> db.replaceAllElementsWithPrefixDemo.insertOne(    {       "StudentNames" : [          "John",          "Carol"       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd91908b50a6c6dd317ad8e") } > > > db.replaceAllElementsWithPrefixDemo.insertOne(    {       "StudentNames" : [          "Sam"       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9191cb50a6c6dd317ad8f") }Following is the query to display all documents from ... Read More

Advertisements