Found 1349 Articles for MongoDB

Find in a dictionary like structure by value with MongoDB?

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

1K+ Views

You can use find() for this. Let us first create a collection with documents −> db.findInDictionaryDemo.insertOne( ...    { ...       "_id":101, ...       "AllCustomerDetails": ...       { ...          "SomeCustomerDetail1": ...          { ...             "CustomerName1":"John Doe", ...             "CustomerName2":"John Smith" ...          }, ...          "SomeCustomerDetail2": ...          { ...             "CustomerName1":"Carol Taylor", ...             "CustomerName2":"David Miller" ... Read More

“Toggle” query in MongoDB?

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

318 Views

You need to find the document and after that you need to use update to toggle query. Let us first create a collection with documents −> db.toggleDemo.insertOne({"CustomerName":"John Smith", "CustomerAge":28, "isMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7be138f9e6ff3eb0ce43b") } > db.toggleDemo.insertOne({"CustomerName":"David Miller", "CustomerAge":25, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7be2e8f9e6ff3eb0ce43c") }Following is the query to display all documents from a collection with the help of find() method −> db.toggleDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc7be138f9e6ff3eb0ce43b"),    "CustomerName" : "John Smith",    "CustomerAge" : 28,    "isMarried" : true } {    "_id" : ObjectId("5cc7be2e8f9e6ff3eb0ce43c"),    "CustomerName" : "David Miller",    "CustomerAge" : 25, ... Read More

Regex to ignore a specific character in MongoDB?

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

803 Views

You can use regular expression along with $not operator to ignore a specific character and display rest of them. Let us first create a collection with documents −> db.regexDemo.insertOne({"CustomerId":"Customer#1234", "CustomerName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7428f8f9e6ff3eb0ce436") } > db.regexDemo.insertOne({"CustomerId":"Customer5678", "CustomerName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc7429e8f9e6ff3eb0ce437") } > db.regexDemo.insertOne({"CustomerId":"Customer#777", "CustomerName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc742ae8f9e6ff3eb0ce438") } > db.regexDemo.insertOne({"CustomerId":"Customer777", "CustomerName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc742bc8f9e6ff3eb0ce439") }Following is the query to display all documents from a collection with the help of find() method −> db.regexDemo.find().pretty();This will produce the ... Read More

How do you perform an AND query on an array in MongoDB?

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

102 Views

To get the same result like AND in MongoDB, use the $all operator. Let us first create a collection with documents −> db.andQueryDemo.insertOne({"StudentName":"Carol Taylor", "FavouriteSubject":["C", "Java", "MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc73e7a8f9e6ff3eb0ce433") } > db.andQueryDemo.insertOne({"StudentName":"David Miller", "FavouriteSubject":["C++", "Java", "MongoDB", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc73ea48f9e6ff3eb0ce434") } > db.andQueryDemo.insertOne({"StudentName":"Carol Taylor", "FavouriteSubject":["Python", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc73ed38f9e6ff3eb0ce435") }Following is the query to display all documents from a collection with the help of find() method −> db.andQueryDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc73e7a8f9e6ff3eb0ce433"),   ... Read More

How to sum every field in a sub document of MongoDB?

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

315 Views

To sum every field in a sub document, use aggregate framework. Let us first create a collection with documents −> db.sumEveryFieldDemo.insertOne( ...    { ...       "_id":101, ...       "PlayerDetails": [ ...          {"PlayerName":"John", "PlayerScore":1000}, ...          {"PlayerName":"Carol", "PlayerScore":2000}, ...          {"PlayerName":"Sam", "PlayerScore":3000} ...       ] ...    } ... ); { "acknowledged" : true,  "insertedId" : 101 }Following is the query to display all documents from a collection with the help of find() method −> db.sumEveryFieldDemo.find().pretty();This will produce the following output −{    "_id" : 101,    "PlayerDetails" : [       {          "PlayerName" : "John",   ... Read More

Check if value exists for a field in a MongoDB document?

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

1K+ Views

To check if value exists for a field in a MongoDB document, you can use find() along with $exists operator. Let us first create a collection with documents −> db.checkIfValueDemo.insertOne({"PlayerName":"John Smith", "PlayerScores":[5000, 98595858, 554343]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f507af8e7a4ca6b2ad98") } > db.checkIfValueDemo.insertOne({"PlayerName":"John Doe", "PlayerScores":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f512af8e7a4ca6b2ad99") } > db.checkIfValueDemo.insertOne({"PlayerName":"Carol Taylor", "PlayerScores":[7848474, 8746345353]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f521af8e7a4ca6b2ad9a") } > db.checkIfValueDemo.insertOne({"PlayerName":"David Miller", "PlayerScores":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6f531af8e7a4ca6b2ad9b") }Following is the query to display all documents from a collection with the help ... Read More

Variable collection name in MongoDB shell with JavaScript?

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

135 Views

Yes, you can set a variable collection name in MongoDB shell using JavaScript. Let us first create a collection with documents −> db.employeeInformation.insertOne({"EmployeeName":"John Smith", "EmployeeAge":24, "EmployeeSalary":450000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6d06baf8e7a4ca6b2ad97") }Following is the query to display all documents from a collection with the help of find() method −> db.employeeInformation.find();This will produce the following output −{ "_id" : ObjectId("5cc6d06baf8e7a4ca6b2ad97"), "EmployeeName" : "John Smith", "EmployeeAge" : 24, "EmployeeSalary" : 450000 }Here is the query to set variable collection name in MongoDB shell using JavaScript.Case 1 − Set variable with varThe query is as follows −> var status=db.employeeInformation; ... Read More

How do I know which MongoDB version is installed using the Command Line?

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

9K+ Views

First open the CMD and then reach the BIN directory of MongoDB. The screenshot to open CMD prompt is as follows.Above we have reached the RUN dialog by pressing START and then typing RUN and ENTER.Now, type CMD and press OK button to get the command line. The screenshot is as follows −Reach the BIN directory of MongoDB. Following is how to reach the BIN −Use the query mongo –version. The screenshot of the query is as follows −Above displays that our current MongoDB version is v4.0.5.

Remove only one document in MongoDB?

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

92 Views

To remove only a single document, use the remove() in MongoDB. Let us first create a collection with documents −> db.removeOnlyOneDocumentDemo.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6ca2f9cb58ca2b005e674") } > db.removeOnlyOneDocumentDemo.insertOne({"FirstName":"Carol", "LastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6ca399cb58ca2b005e675") } > db.removeOnlyOneDocumentDemo.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6ca429cb58ca2b005e676") }Following is the query to display all documents from a collection with the help of find() method −> db.removeOnlyOneDocumentDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6ca2f9cb58ca2b005e674"),    "FirstName" : "John",    "LastName" : "Smith" } {    "_id" : ObjectId("5cc6ca399cb58ca2b005e675"), ... Read More

How to remove key fields in MongoDB?

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

1K+ Views

To remove key fields in MongoFB, you can use $unset operator. Let us first create a collection with documents −>db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6c8289cb58ca2b005e672") } >db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6c8359cb58ca2b005e673") }Following is the query to display all documents from a collection with the help of find() method −> db.removeKeyFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6c8289cb58ca2b005e672"),    "StudentFirstName" : "John",    "StudentLastName" : "Doe",    "StudentAge" : 23 } {    "_id" : ObjectId("5cc6c8359cb58ca2b005e673"),    "StudentFirstName" : "John",    "StudentLastName" : "Smith",   ... Read More

Advertisements